Skip to content Skip to sidebar Skip to footer

Problem With Dynamicallly Adding Values To Dropdown Box

i want to dynamically add options to drop down boxes var x =document.getElementById('c'); var optn = document.createElement('OPTION'); optn.text='hhh' optn.value='val

Solution 1:

Try this one:

var objSelect = document.getElementById("subComponentOSID");
objSelect.options[objSelect.options.length] = newOption('1','1');
objSelect.options[objSelect.options.length] = newOption('2','2');

Solution 2:

add is a method of HTMLSelectElement objects, not of HTMLCollection objects.

x.add(optn)

Solution 3:

Assuming the element with the id "subComponentOSID", the only apparent issues in your javascript are missing semicolons on the lines where you assign values to optn.text and optn.value. Also, while most browsers will resolve what you mean when calling the add function on an options collection for a select element, you should move your add to the select itself. See the Mozilla reference for HTMLSelectElement, which provides an example.

In the meantime, try replacing the code snippet you provided with this:

var x =document.getElementById("subComponentOSID");
var optn = document.createElement("OPTION");
optn.text="hhh";  //Added semi-colon
optn.value="val";  //Added semi-colon
x.add(optn);  // Moved add to HTMLSelectElement

Post a Comment for "Problem With Dynamicallly Adding Values To Dropdown Box"