Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

HTML element: datalist

The suggestion-list element supplies a form control with a non-binding set of proposed values while allowing another valid value to be entered.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

<input list="cities"><datalist id="cities"><option value="Oslo"></datalist>

datalist connects to a compatible input through a list attribute equal to its id; HTMLInputElement.list returns the resolved element or null.

Suggestions come from descendant option elements: value is the value to insert, label can add a description, and empty or disabled options are not suggestions.

datalist is not select and does not require a choice. Constraints such as required and pattern validate the final value independently, while browser UI and filtering vary.

The example counts candidates through options, enters an unlisted value, and calls checkValidity() to confirm that value is not limited to the suggestions.

Examples

<!doctype html>
<meta charset="utf-8">
<label>City <input id="city" name="city" list="cities" value="Oslo"></label>
<datalist id="cities">
  <option value="Oslo"></option>
  <option value="Tokyo"></option>
  <option value=""></option>
  <option value="Lima" disabled></option>
</datalist>
<output id="result"></output>
<script>
const city = document.querySelector("#city");
const source = document.querySelector("#cities");
const suggestions = [...source.options].filter(option => option.value && !option.disabled);
city.value = "Reykjavík";
document.querySelector("#result").textContent = `linked=${city.list === source}; options=${source.options.length}; suggestions=${suggestions.length}; unlisted=${city.value}; valid=${city.checkValidity()}`;
</script>

Result

linked=true; options=4; suggestions=2; unlisted=Reykjavík; valid=true

The result verifies the connection, two usable suggestions, and a valid unlisted value; it does not assess the native popup’s appearance.

Sources