Click to See Complete Forum and Search --> : for loops and select boxes


Superfly1611
11-05-2003, 06:29 AM
I need to know how i'd go about selecting a Select box and then looping through the options until i found the option with a certain value and then make that option selected

for example i have a select box with 3 options
Option/Value
A / 1
B / 2
C / 3

The javascript needs to loop through the options until it finds an option with the value "2" for example and then Select that option (B - in this case).

Any ideas?

Thanks

gil davis
11-05-2003, 06:44 AM
Something like this:

for (var i=0; i<document.formName.selectName.length; i++;)
{if (document.formName.selectName.options[i].value == "whatever")
{document.formName.selectName.options[i].selected = true;}
}

Superfly1611
11-05-2003, 07:20 AM
cheers for that gil - will give it a go