Datalist (Input Suggestions) — HTML

A <datalist> is a list of suggestions that appears while a visitor types in an input. Connect the two with <input list="x"> and <datalist id="x">, using…

Type "lo" into Google's search box, and suggestions appear right under it. The input boxes in your form can show suggestions like Google's search box too. You add them with a <datalist>: a list of suggestions for one input box. As soon as a visitor starts typing, the browser shows the matching suggestions under the box. The visitor can click a suggestion, or keep typing their own answer.

An input with a list of suggestions

<label for="city">City</label>
<input id="city" name="city" list="city-list">

<datalist id="city-list">
  <option value="London">
  <option value="Manchester">
  <option value="Leeds">
  <option value="Bristol">
</datalist>

This code has two connections: the label to the input, and the input to the datalist. Both use an id. Read the tags one at a time:

So one input has two connecting attributes, and each has its own job: id connects the input to the label, list connects it to the datalist. The datalist is a separate tag, written after the input. The datalist can't go inside the input: an input has no closing tag, so it has no inside.

The result: type "lo" and the browser offers London. Click the suggestion, and the box is filled with London.

Suggestions, not rules

A datalist only makes suggestions. It does not limit the answer. A visitor can still type York even though that city isn't on the list, and that answer is sent with the form.

If visitors must pick one of the options and may not type their own answer, use <select>:

<select id="city" name="city">
  <option value="London">London</option>
  <option value="Manchester">Manchester</option>
</select>

So the difference is:

⚠️ Suggestions that don't show up, or don't behave as you expect

TL;DR: A <datalist> is a list of suggestions that appears while a visitor types in an input. Connect the two with <input list="x"> and <datalist id="x">, using exactly the same value. Then fill the datalist with <option value="...">, one for each suggestion. The input keeps its own id for the label. Visitors can pick a suggestion or type anything. If they must pick from the list, use <select>.

⚡ Suggestions as you type

Write:

<label for="city">City</label>
<input id="city" name="city" list="city-list">

<datalist id="city-list">
  <option value="London">
  <option value="Manchester">
  <option value="Leeds">
</datalist>

Meaning: A <datalist> is a list of suggestions for an input. The input's list must be exactly the same as the datalist's id. Type "lo" and the browser offers London. The input keeps its own id for the label, and the datalist goes after the input, not inside it.

⚡ Suggestions, not rules

Guess first: a visitor types York, which isn't on the list. Is it rejected?

No. The answer is still sent with the form. A datalist only makes suggestions.

Meaning: With a <datalist>, visitors may type anything. It is good for cities or search words. If visitors must pick one of the options, use <select>.

What you will learn