When a text field has a few common answers but also needs free text, a <select> is too strict. <datalist> gives suggestions while still letting people type anything. I like that it needs no script at all.
Link an input to a list with the list attribute, which points at the id of the datalist:
<label for="browser">Browser</label>
<input list="browsers" id="browser" name="browser">
<datalist id="browsers">
<option value="Firefox">
<option value="Safari">
<option value="Chrome">
<option value="Microsoft Edge">
</datalist>
As the visitor types, matching options appear in a dropdown. Picking one fills in its value. The same works on search, url, tel, email and number inputs, and on date, time and color inputs the options can show up in the browser’s own picker. For a range input, the options become tick marks.
Caveats worth knowing before you use it for anything serious:
- The list is only a suggestion. People can still submit any value that passes validation, so check on the server or add
patternor similar attributes. If the value must come from the list, use<select>. - MDN lists accessibility gaps: the options do not scale with browser zoom, you can barely style them, and some screen reader and browser combinations do not announce the popup.
- MDN does not mark it as Baseline, because support is uneven in some widely used browsers, so look at the compatibility table before relying on it.
Since it degrades to a plain text input, it is a safe extra for things like a country, a city or a tag field.
Further reading: MDN: <datalist>