Sobes.tech
Junior

What is the <datalist> element in HTML5?

sobes.tech AI

Answer from AI

<datalist> is an HTML element that provides a list of predefined options for the <input> element.

Main features:

  • It is associated with <input> via the list attribute (<input list="myOptions">).
  • It contains <option> elements, each representing a single option.
  • It is not displayed itself, but serves as a data source for <input>.
  • It offers a dropdown list of possible values as the user types in <input>.
  • It allows entering values not present in the list (<datalist>), unlike <select> elements, which restrict choices to predefined options.

Example usage:

<!-- Linking input with datalist by id "browserOptions" -->
<input list="browserOptions" name="browser">

<datalist id="browserOptions">
  <option value="Chrome">  <!-- One of the options -->
  <option value="Firefox">
  <option value="Safari">
  <option value="Edge">
  <option value="Opera">
</datalist>