akshay
akshay

Reputation: 755

Dynamically adding options to drop down box

I have a drop down box in which values are entered dynamically. But sometimes it's value does not get refreshed. How can I force the drop down box to refresh?

var DropdownBox =document.getElementById("xyz");
var optn = document.createElement("OPTION");
optn.text="txt";
optn.value="val";
DropdownBox.options.add(optn);

Upvotes: 2

Views: 5840

Answers (3)

derekcohen
derekcohen

Reputation: 1514

This is what I use:

var target=document.getElementById('myselect');    
var optionName = new Option('option text', 'option value');    
var targetlength = target.length;    
target.options[targetlength] = optionName; 

Upvotes: 0

lonesomeday
lonesomeday

Reputation: 238065

That should be DropdownBox.add(optn);, I believe. See the MDC page describing HTMLSelectElement.

Upvotes: 1

Felix Kling
Felix Kling

Reputation: 817128

Have you tried

DropdownBox.appendChild(optn);

?

Afaik options.add() is only supported in IE.

Upvotes: 0

Related Questions