2

Am unable to add elements to a dropdown list via Javascript.

The below piece of code works in IE and Chrome, but not in firefox.

ddlId.add(new Option("",0));

In firefox, I keep getting an 'Not enough arguments' exception. Any idea on how to resolve it? Thanks

4 Answers 4

4
try {
    ddlId.add(new Option("",0), null);  // standards compliant; doesn't work in IE
} catch(ex) {
    ddlId.add(new Option("",0));    // IE only
}
Sign up to request clarification or add additional context in comments.

Comments

2

Hm. The idea is, roughly, to go to the Mozilla Developer Center page for select.add() and have a look at the method signature ;-)

Syntax

select.add(newOption, existingOption);

Parameters

newOption
An HTMLOptionElement to add to the options collection.

existingOption
An existing HTMLOptionElement within the collection used as a reference point for inserting the new element; the new element being inserted before the referenced element in the collection. If this parameter is null, the new element is appended to the end of the collection.

1 Comment

Thanks! Will remember to check out there in case of any firefox related JS issues!! :-)
1
var opt = document.createElement("option");
  var ddlPopulate=document.getElementById("<%=ddlPopulate.ClientId %>");
opt.text="firstElement";
  opt.value="1";
  ddlPopulate.options.add (opt);

Comments

0

The select element has as its children an options array. You add or remove options as you would using standard array methods.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.