How to set a select option as selected by value in JavaScript
Codemia Knowledge Hub ·
Assign the value to the <select> element's value property. The browser matches it against the available <option> elements and updates the selection. Multi-select controls, dynamic option lists, and framework-managed forms each need a slightly different approach, covered below.
Assigning select.value
Given this markup:
Select the banana option with one line:
The browser iterates through the options collection, finds the first <option> whose value matches, sets its selected property to true, and clears the previously selected option. For single-select controls you never need to loop manually.
The match is against the value attribute, not the visible label. For <option value="us">United States</option>, set select.value = "us", not "United States".
Verifying that a value matched
If the assigned value matches no option, every option is deselected: selectedIndex becomes -1 and a single-select shows an empty selection. No exception is thrown. When the value comes from external data (an API, user input, a URL parameter), check the outcome:
This matters most when options are rendered from server data that might be out of sync with the client.
Selecting several values in a <select multiple>
For a <select multiple>, value only reflects a single value. Iterate through options and set selected on each one:
A Set keeps lookup time constant regardless of how many values you match, which helps when both lists are large.
To read back all selected values:
Firing the change event
Setting select.value programmatically updates the DOM but does not fire change. If validation logic, dependent dropdowns, or analytics rely on that event, dispatch it yourself:
{ bubbles: true } lets listeners on ancestor elements receive the event, matching native behavior when a user interacts with the control. Forgetting this leads to dependent UI elements getting out of sync.
Querying the option element directly
When you need the option node itself, query for it:
This is more verbose than select.value, but useful when you also want to add a CSS class to the option, read its textContent, or check whether it is disabled before selecting.
Options loaded asynchronously
When options are fetched from an API or rendered by a template engine, wait until they exist before setting the value:
Setting a value before the matching option exists fails silently with no error message. It is one of the most frequent bugs in form initialization code and hard to debug in production.
React, Vue and Angular
In frameworks, selection is driven by component state rather than direct DOM manipulation:
You "select by value" by setting the state variable, and the framework updates the DOM. In React, placing selected on individual <option> elements instead of using the value prop on <select> produces a console warning and inconsistent behavior.
The selected attribute versus the property
Setting the selected HTML attribute and expecting it to stay authoritative after JavaScript modifies the control is a common mistake. Once the page is interactive, the DOM property (option.selected) takes precedence over the original markup attribute.
Comparing the selection methods
| Method | Best For | Fires change Event? | Works With Multi-Select? |
select.value = "..." | Single-select, simple cases | No | No (single value only) |
option.selected = true | Multi-select or per-option logic | No | Yes |
querySelector + selected | Need to inspect option before selecting | No | Yes |
Framework binding (v-model, value) | Framework-managed forms | Via framework event system | Depends on framework |
dispatchEvent(new Event("change")) | Triggering downstream handlers | Yes (manually) | N/A (used after any method) |
Related reading
- Set TextView text from html-formatted string resource in XML
- Set useState hook in a async loop
- setTimeout in React Native
- sh 1 react-scripts not found In Docker
- sh husky command not found
- Should CSS always precede JavaScript?
- Should I add async/await to a single-line function or not?
- Should I always use redux-saga call effect for functions that return promise?
System design interview problems on this topic
- Design Google Doc: solution and walkthrough
- Design Facebook Messenger: solution and walkthrough
- Design Typeahead Suggestion: solution and walkthrough
- Design an Online Presence Indicator Service: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.