Javascript Set Selected Index For Dropdown

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.

Donations appreciated. Every little $ helps. Or click Google +1

Related Posts with Thumbnails

3 Responses to “Javascript Set Selected Index For Dropdown”

  1. 1
    poes Says:

    jy is ‘n poes, vestaan jy dat jy ‘n doos poes is

  2. 2
    Shiba Says:

    Superb solution!

  3. 3
    tech Says:

    @shiba: thanks

Leave a Reply

Spam protection by WP Captcha-Free