Full answer
Pick the control by how many choices there are and whether free text is allowed.
- Radio buttons suit a small set of options, about two to five, where seeing all of them helps. The user picks exactly one.
selectsuits a longer fixed list, such as a country. The value must be one of the options.optgroupgroups the options under a label, andmultipleallows several picks, although a list of checkboxes is often easier to use.datalistworks with a textinputthrough itslistattribute. It suggests values while the user types, but the user can still type anything. Use it for suggestions, for example a city field, and do not use it when the value must come from the list.
Points that matter:
- A
selecthas a first option with an empty value such as "Choose a country" when the field is required, so the user must make a choice. optiontext is what the user sees. Thevalueattribute is what is sent. Ifvalueis missing, the text is sent.datalistoptions do not make a field valid or invalid. Addpatternor script checks if the value must match.selectis styled by the browser.appearanceand a custom arrow can change the look, but the popup list stays mostly native, and that is a good thing for keyboard and screen reader users.
Setting the select to a value that is not in the list leaves it empty, and the required check fails. The city field keeps any text, even "Zulu", because the list only suggests.
Always add a label, because the options do not name the field.
html
<form id="f">
<label for="c">Country</label>
<select id="c" name="country" required>
<option value="">Choose a country</option>
<optgroup label="Asia"><option value="in">India</option><option value="jp">Japan</option></optgroup>
<optgroup label="Europe"><option value="de">Germany</option></optgroup>
</select>
<label for="city">City</label>
<input id="city" name="city" list="cities">
<datalist id="cities"><option value="Pune"><option value="Paris"></datalist>
</form>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Radio for a few options, select for a long fixed list.
- datalist suggests but does not limit.
- optgroup groups options under a label.
- An empty first option makes required useful.
- Always connect a label.
Common mistakes
- Using datalist when the value must be one of the options. It never enforces that.
- Forgetting the empty first option on a required select, so the first real option looks pre-chosen.
- Using a native select with hundreds of options when a search box would be faster.
Follow-ups
- How does a select send its value?
- When is a list of checkboxes better than select multiple?
Tips
- Choose by size of list and whether free text is allowed.
- State that datalist only suggests.