← Back to HTML & CSS, page 6

html-css-0107 · Medium mid

When would you use select, datalist or radio buttons for a choice, and how do their values behave?

Use radio buttons for a few options, select for a longer fixed list, and datalist for suggestions in a text input. A select value must be one of its options, while a datalist input accepts any text. Add an empty first option and a label.

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.
  • select suits a longer fixed list, such as a country. The value must be one of the options. optgroup groups the options under a label, and multiple allows several picks, although a list of checkboxes is often easier to use.
  • datalist works with a text input through its list attribute. 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 select has a first option with an empty value such as "Choose a country" when the field is required, so the user must make a choice.
  • option text is what the user sees. The value attribute is what is sent. If value is missing, the text is sent.
  • datalist options do not make a field valid or invalid. Add pattern or script checks if the value must match.
  • select is styled by the browser. appearance and 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.