Feedback Form

Saturday, July 05, 2008

Select an option in select tag by value

<SELECT name="fruit">
<OPTION value="apple">apple</option>
<OPTION value="orange">orange</option>
<OPTION value="pear">pear</option>
</SELECT>

If you need to select an option in a select tag by index you can do the following:

document.getElementById('dropDown').selectedIndex = 1;


But if you want to select an option in a select tag by value you can do the following:

for(var i=0; i<document.getElementById('dropDown').options.length;i++)
        if(document.getElementById('dropDown').options[i].value == "MYVALUE")
document.getElementById('dropDown').selectedIndex = i;



kick it on DotNetKicks.com