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,

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.

Related Posts Plugin for WordPress, Blogger...

8 comments

  1. Thank you for such a simple solution. I’ve been searching for about an hour or two. All the solutions I’ve seen were overly complex. This is exactly what I was looking for and worked like a charm.

Leave a Reply

Your email address will not be published. Required fields are marked *