开发者

Adding elements to dropdownlist through javascript

开发者 https://www.devze.com 2023-01-03 05:25 出处:网络
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.

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


try {
    ddlId.add(new Option("",0), null);  // standards compliant; doesn't work in IE
} catch(ex) {
    ddlId.add(new Option("",0));    // IE only
}


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.


var opt = document.createElement("option");
  var ddlPopulate=document.getElementById("<%=ddlPopulate.ClientId %>");
opt.text="firstElement";
  opt.value="1";
  ddlPopulate.options.add (opt);


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

0

精彩评论

暂无评论...
验证码 换一张
取 消