Selecting an option with javascript
Posted
by
Fibericon
on Stack Overflow
See other posts from Stack Overflow
or by Fibericon
Published on 2012-09-03T03:31:38Z
Indexed on
2012/09/03
3:38 UTC
Read the original article
Hit count: 165
I have a drop down menu that I want to have selected on page load. This is what I'm using at the moment:
var selector = document.getElementById("action_person").firstChild;
var n = 0;
while(selector.options[n] != null)
{
if(selector.options[n].value == "person")
{
selector.options.selectedIndex = n;
}
n++;
}
I've also tried replacing selector.options.selectedIndex = n
with selector.options[n].selected = true
. However, it never selects for me. It always shows the item at the top of the drop down.
I've verified that the value "person" does exist in the drop down, and that the variable "selector" does point to a valid drop down. What am I doing wrong here?
© Stack Overflow or respective owner