views:

112

answers:

3

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

+1  A: 
try {
    ddlId.add(new Option("",0), null);  // standards compliant; doesn't work in IE
} catch(ex) {
    ddlId.add(new Option("",0));    // IE only
}
Mark Baker
Thanks! Not sure why the browsers are so incompatible!!!
A: 

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

Robusto
+2  A: 

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.

Tomalak
Thanks! Will remember to check out there in case of any firefox related JS issues!! :-)