Javascript Set Selected Index For Dropdown
Posted by tech on
August 12, 2008
There are lots of ways to set a dropdown’s selection but the easiest would be to set the dropdown’s value with a value. Provided you used an id to access the dropdown object, do this.
document.getElementById('dropdown').value = 'one';
If your dropdown looks like this,
<select id="dropdown">
<option value="one">One</option>
<option value="two">Two</option>
</select>
Then using the Javascript code above, the selection will highlight One. The value attribute must be present. If not, then the dropdown item’s label name gets used. If you wish to use other means, you can check out selectedIndex.











March 24th, 2011 at 6:06 am
jy is ‘n poes, vestaan jy dat jy ‘n doos poes is
August 22nd, 2011 at 8:34 am
Superb solution!
August 22nd, 2011 at 9:22 am
@shiba: thanks