Question
I expected that adding a value attribute to the <select> element would automatically select the matching <option> by default.
<select name="hall" id="hall" value="3">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
However, this does not work as expected. How can I set which <option> element is selected by default in an HTML <select> element?
Short Answer
By the end of this page, you will understand how default selection works in an HTML <select> element, why putting value on <select> does not set the initial choice in plain HTML, and how to correctly choose a default option using the selected attribute or JavaScript when needed.
Concept
A <select> element contains a list of <option> elements. The selected item is not determined by a value attribute on the <select> tag in plain HTML markup. Instead, the browser decides the initial selected option based on the <option> elements inside it.
In static HTML, the default choice is usually set by adding the selected attribute to the correct <option>.
<select name="hall" id="hall">
<option>1</option>
<option>2</option>
<option selected>3</option>
<option>4</option>
<option>5</option>
</select>
A more explicit version uses value on each option:
Mental Model
Think of a <select> like a menu with several items, and each <option> is one item on that menu.
The browser asks: which menu item should start checked?
It does not look at the <select> tag and say, "I see value="3", so I will pick 3." Instead, it looks through the <option> tags and checks whether one of them is marked as the starting choice.
So:
<select>is the container.<option>is the actual choice.selectedis the sticker that says, "start with this one."
If no option is marked selected, the browser usually picks the first one by default.
Syntax and Examples
Basic syntax
Use selected on the option you want chosen initially.
<select name="hall" id="hall">
<option value="1">1</option>
<option value="2">2</option>
<option value="3" selected>3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
In this example, the option with value 3 is selected when the page first loads.
Why your original code did not work
1
2
3
4
5
Step by Step Execution
Consider this example:
<select id="hall">
<option value="1">1</option>
<option value="2">2</option>
<option value="3" selected>3</option>
<option value="4">4</option>
</select>
Here is what happens step by step:
-
The browser reads the
<select>element. -
It reads each
<option>inside it. -
It checks whether any option has the
selectedattribute. -
It finds:
< = >3
Real World Use Cases
Default selected options are common in real applications.
Edit forms
When editing a user profile, you may need to show the saved country, role, or department.
<select name="role">
<option value="user">User</option>
<option value="admin" selected>Admin</option>
<option value="editor">Editor</option>
</select>
Placeholder choices
A form may start with a prompt such as "Choose a category".
<select name="category" required>
<option value="" selected>Choose a category</option>
<option value=>Books
Music
Real Codebase Usage
In real projects, developers often use a few common patterns around <select> elements.
Server-rendered forms
A backend template marks the correct option as selected.
Example idea:
<option value="admin" selected>Admin</option>
This is common when editing existing records.
JavaScript state binding
Frameworks and plain JavaScript often set the current selection from application state.
select.value = userSettings.hall;
This is useful when data arrives after page load.
Guard clauses for invalid values
Developers often check whether a value exists before assigning it.
if (savedValue) {
select.value = savedValue;
}
This avoids accidentally selecting an empty or unintended option.
Validation patterns
A placeholder option with an empty value is often paired with required.
Common Mistakes
Mistake 1: Putting value on <select> in plain HTML and expecting it to preselect an option
Broken example:
<select value="3">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
Why it is a problem:
- This does not set the initial selected option in static HTML.
Fix:
<select>
<option value="1">1</option>
<option value="2">2</option>
3
Comparisons
| Approach | Where it works | Best use case | Example |
|---|---|---|---|
selected on <option> | Static HTML | Set the initial default in markup | <option value="3" selected>3</option> |
select.value = '3' | JavaScript | Change selection after page load | document.getElementById('hall').value = '3' |
| First option fallback | Browser default behavior | When no option is explicitly selected | First option is chosen |
selected vs value on <select>
Cheat Sheet
Quick rules
- To set the default option in HTML, use
selectedon the correct<option>. - Do not rely on
valueon the<select>tag in plain HTML to choose the default option. - In JavaScript, use
select.value = '...'to change the selected option. - If no option is marked
selected, the first option is usually selected.
Static HTML
<select name="hall">
<option value="1">1</option>
<option value="2">2</option>
<option value="3" selected>3</option>
</select>
JavaScript
.(). = ;
FAQ
Why does value="3" on <select> not select the option in HTML?
Because the initial selection is determined by the <option> elements, not by the value attribute in static markup on <select>.
How do I set the default selected option in HTML?
Add the selected attribute to the option you want selected by default.
<option value="3" selected>3</option>
Can I change the selected option with JavaScript?
Yes. Use the select element's value property.
document.getElementById('hall').value = '3';
What happens if no option is marked selected?
In a normal dropdown, the browser usually selects the first option.
Should I always add value to each ?
Mini Project
Description
Build a simple booking form dropdown for selecting a hall number. This project demonstrates both ways of setting a default selection: directly in HTML with selected, and dynamically in JavaScript using the select element's value property.
Goal
Create a hall selector that starts with a default option and can also be updated programmatically.
Requirements
- Create a
<select>element with at least five hall options. - Set one default option using HTML.
- Add a button that changes the selected hall using JavaScript.
- Show the currently selected hall below the dropdown.
Keep learning
Related questions
Can You Style Half a Character in CSS? Text Effects with CSS and JavaScript
Learn how to style half of a character using CSS and JavaScript, including overlay techniques for dynamic text effects.
Get Screen, Page, and Browser Window Size in JavaScript
Learn how to get screen size, viewport size, page size, and scroll position in JavaScript across major browsers.
Get the Selected Radio Button Value with jQuery
Learn how to find which radio button is selected in jQuery and get its value with simple examples and common mistakes.