Forum Moderators: open
I have list box: mylistbox
I can get the listbox:
var listBox = document.getElementById("mylistbox");
but now I want to get the index of an option based on the value so I can select it dynamically.
So say I dynamically set the options to:
<option value="14">apples</option>
<option value="22">oranges</option>
BTW, javascript var myvar = 22;
Now I want to select the value "22" in the options and have that show in the list box the one that is selected.
I tried:
var po = listBox.getElementsByTagName("option");
var myvalue = po[myvar].value;
Doesn't work.
And I can't seem to dump 'po' because all I get is [object].
And is there a solid book that thoroughly covers issues like this?
Thanks,
I guess I have to loop through it all.
for (var x = 0; x < mylen; x++) {
if (listBox.options[x].value == myvar) {
listBox.options[x].selected = true;
}
}