-
SitePoint Zealot

Setting SELECT option value
Hi All,
In JS can you set the value of a SELECT dropdown box by text value, not index value?
The following will set the SELECT box by the index value, selecting the second value in the dropdown box
var varyear = 1
document.myform.year.options[varyear].selected = true;
however i want to set it like this...
var varyear = 2006
document.myform.year.options[varyear].selected = true;
...to set the SELECTED value to 2006...which doesn't work
Thanks for any help
Melt
-
SitePoint Enthusiast
Might it be the case of looping through each of the SELECTs options and comparing its value to your string? Something like:
for (i=0; i<myform.year.options.length; i++) {
if (myform.year.options[i],value == varyear) {
myform.year.options[i].selected = true;
break;
}
}
This is just off the top of my head and hasnt been tested.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
Bookmarks