Adding and reading values from combo box using Javascript
By Anuraj P on May 11th, 2007 . 7 Comments .
If you are using Ajax Pro or MS Ajax with Page methods(Available in ASP.Net 3.5), you may want to fill a dropdown list from client side. This code snippet will help you to add items to dropdownlist(combobox) in runtime using Javascript.
Code to Add an option to combo using Javascript.
var combobox = document.getElementById("combobox");
for (var i=0; i < 10; i++)
{
combobox.options[i] = new Option(i,i); //This code will work.
combobox.options.add(new Option(i,i));//This code will also work.
}
This will create 10 items in the combobox. And to read from the combo box
var combobox = document.getElementById("combobox");
for(var i = 0; i < combobox.options.length; i++)
{
alert(combobox.options[i].value);
}
This will alert all the 10 values from the combo box.