Set value of a drop-down list with JavaScript/jQuery

Google Translate Icon

This post will discuss how to set the value of a dropdown list in JavaScript and jQuery.

1. Using jQuery

The jQuery’s .val() method allows setting the value of a dropdown.

JS


HTML



Edit in JSFiddle

 
Note that .val() does not fire the change event. To trigger the change event, you can call the .change() or .trigger("change") method after setting the value.

 
Alternatively, you can use the .prop() method to set the selectedIndex property which reflects the index of the selected <option> element.

JS


HTML



Edit in JSFiddle

 
The selectedIndex property takes an index of the value to be set. If the index is not known in advance or you don’t want to depend on the index, you can do like:

JS


HTML



Edit in JSFiddle

2. Using JavaScript

In pure JavaScript, you can use the selectedIndex property which reflects the index of the selected <option> element.

JS


HTML



Edit in JSFiddle

 
Alternatively, you can loop through each <option> and set the desired value:

JS


HTML



Edit in JSFiddle

That’s all about setting the value of a drop-down list in JavaScript and jQuery.

Rate this post

Average rating 4.67/5. Vote count: 27

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Tell us how we can improve this post?




Thanks for reading.

Please use our online compiler to post code in comments using C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.

Like us? Refer us to your friends and help us grow. Happy coding :)



Subscribe
Notify of
guest
3 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Do NOT follow this link or you will be banned from the site!