The <datalist> element provides a list of suggested values for an <input>. Users can pick a suggestion or type any custom value. This improves UX by reducing typing and errors while still allowing flexible input.
You connect an <input> to a <datalist> using the input’s list attribute and the datalist’s id. Each suggestion is an <option> inside the datalist.
list attribute to the datalist's id.<option value="..."> per suggestion (text comes from the value).
<!-- Input connected to a datalist by list/id -->
<input type="text" list="my-options">
<datalist id="my-options">
<option value="First choice"></option>
<option value="Second choice"></option>
</datalist>
<!-- Input connected to a datalist -->
<label for="browser">Favorite browser</label>
<input type="text" id="browser" name="browser" list="browsers"
placeholder="Choose or type...">
<datalist id="browsers">
<option value="Google Chrome"></option>
<option value="Mozilla Firefox"></option>
<option value="Safari"></option>
<option value="Microsoft Edge"></option>
<option value="Opera"></option>
</datalist>
Because only the input's text is submitted, you can use a hidden field to capture a code when a suggestion is chosen. Here we show country name and dial-code to the user, but submit a short country code.
<label for="country">Country</label>
<input type="text" id="country" name="country_display" list="countries"
placeholder="Type or choose...">
<input type="hidden" id="country_code" name="country_code">
<datalist id="countries">
<option value="India (+91)" data-code="IN"></option>
<option value="United States (+1)" data-code="US"></option>
<option value="Japan (+81)" data-code="JP"></option>
<option value="Germany (+49)" data-code="DE"></option>
</datalist>
{ }
list attribute to the datalist's id.required, pattern and server-side checks if you need fixed values.text, search, email, tel); it’s ignored by some types (like number).data-code and update the preview to include dial codes.pattern with datalist so only values like Word (+NN) are accepted.@.<datalist> adds suggestions to an <input> without restricting custom values.list and the datalist’s id.<option> whose value is what appears in the input.