Skip to main content

HTML Tags

HTML <datalist> Tag

Written by Updated HTML <datalist> Tag exercises

The datalist tag gives an input box a list of suggestions that pop up as you type, while still letting people type anything they like. It is autocomplete without JavaScript.

That last part is the difference from a select drop-down: a select forces a choice from the list; a datalist only suggests. The suggestions are written with the option tag.

Syntax

html

<input list="listId" name="inputName" id="inputId">
<datalist id="listId">
  <option value="Option 1">
  <option value="Option 2">
</datalist>

The input's list attribute must match the datalist's id. That is the only connection between them.

AttributeWhat it does
idThe name the input refers to in its list attribute. Required.

Example

html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Datalist Example</title>
</head>
<body>
  <h2>Choose Your Favorite Browser</h2>
  <form action="/samples/form_processor.php" method="post">
    <label for="browser">Browser:</label>
    <input list="browsers" id="browser" name="browser" placeholder="Start typing..." required>
    <datalist id="browsers">
      <option value="Chrome">
      <option value="Firefox">
      <option value="Edge">
      <option value="Safari">
      <option value="Opera">
      <option value="Internet Explorer">
    </datalist><br><br>

    <input type="submit" value="Submit">
  </form>
</body>
</html>

Click the field or start typing and the browser names appear as suggestions. Type "Brave" instead and that works too; the form accepts any value.

Good to know

  • The suggestion list looks different in every browser and cannot be styled with CSS. If you need a custom look, you need a JavaScript component.
  • If only the listed values are allowed, use <select>, or check the value on the server.
  • Datalist also works with other input types, such as type="color", type="range" (it shows tick marks in some browsers) and type="email".

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE9+
YesYesYesYesYesYes

HTML <datalist> Tag exercises

Offer suggestions

Add a text field named city that suggests London and Delhi, from a list with the id cities. The visitor may still type anything.

Result

Check your answer to see it here.