Understand how the <option> element controls both what users see in a dropdown and what data is sent to the server.
What it is
The <option> element defines an item within a <select>, <datalist>, or <optgroup>. It represents a single choice in a form. The critical concept is the separation between the displayed text (what the user reads) and the submitted value (what the browser sends).
If no value attribute is present, the submitted value defaults to the displayed text. If a value attribute is present, that specific string is submitted instead of the visible text.
Why it matters
- Data Integrity: Allows you to send clean IDs (e.g., "101") while showing friendly names (e.g., "New York").
- Localization: You can display translated text to users while submitting standard English codes or database keys.
- Backend Logic: Servers often expect specific enum values or foreign keys rather than arbitrary user-facing strings.
- Accessibility: Clear labels help screen readers announce options correctly, distinct from the underlying data.
Syntax or steps
An option requires opening and closing tags. Inside, you place the visible text. To control submission, add the value attribute.
- Create a
<select>container with anameattribute. - Add
<option>elements inside. - Set the
valueattribute for each option if it differs from the text. - Use the
selectedboolean attribute to pre-select an option.
Example
<form action="/submit" method="post">
<label for="city">Choose your city:</label>
<select id="city" name="city_id">
<option value="">-- Select --</option>
<option value="NYC">New York City</option>
<option value="LA" selected>Los Angeles</option>
<option value="CHI">Chicago</option>
</select>
<button type="submit">Send</button>
</form>
Part-by-part explanation:
<select name="city_id">: The form field name iscity_id.<option value="NYC">New York City</option>: The user sees "New York City", but if selected, the browser submitscity_id=NYC.<option value="LA" selected>...</option>: Los Angeles is chosen by default when the page loads.<option value="">-- Select --</option>: A placeholder option with an empty value, useful for validation.
Common mistakes
- Forgetting the
valueattribute: If you omitvalue, the entire text content becomes the submitted data. This breaks backend logic expecting short codes. - Mismatched
nameattributes: Ensure the<select>has aname. Options do not have their ownname; they inherit the parent's name. - Using
checkedinstead ofselected: Radio buttons usechecked; options useselected. - Empty values without intent: An option with
value=""submits nothing. Use this deliberately for placeholders, not accidentally.
When to use it
Compare <option> with radio buttons (<input type="radio">) for small sets of choices.
| Feature | <option> (Select) | <input type="radio"> |
|---|---|---|
| Space Efficiency | High (collapsible) | Low (all visible) |
| Best For | Many choices (>5) | Few choices (2-4) |
| Value Control | Explicit via value | Explicit via value |
| Mobile UX | Native picker UI | Tappable circles |
Practice
Guided Exercise: Create a select menu for "T-Shirt Size". Display "Small", "Medium", "Large". Submit values "S", "M", "L". Pre-select "Medium".
Challenge: Add a disabled option at the top saying "Please choose a size" with no value.
Hint: Use <option disabled> for the challenge part.
Quick check
Q: If an option is written as <option>Red</option>, what value is submitted?
A: The string "Red". Without a value attribute, the text content is used.
Summary
The <option> element decouples user experience from data processing. Always define explicit value attributes to ensure your backend receives predictable, machine-readable data regardless of how the label appears to the human user.