Select a Dropdown List Item Using JavaScript

The following JavaScript functions are useful for selecting items in HTML dropdown lists. These can be used especially if you have server-side code returning values that should then be visually selected on the user’s screen without rebuilding the entire page.

This first function will select an item based on its value. Pass in an object referring to a list element, e.g. document.getElementById(“country_list”), and the value that should be selected.

This second function will select an item based on its displayed text name. Pass in an object referring to a list element, e.g. document.getElementById(“country_list”), and the displayed text value that should be selected.

The following is an example of how to call both functions:

Leave a Comment