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:
<label for="city">is the name of the box, the text visitors read. It works the same as in the input and label lesson: the label'sforhas the same value as the input'sid="city". That is the first connection.<input ... list="city-list">is the box. Itslistattribute holds the id of the suggestion list. That is the second connection.<datalist id="city-list">is the suggestion list. Itsidmust be exactly the same as the input'slist, including capital and small letters. The list stays hidden until the visitor types in the box.<option value="London">is one suggestion. The list shows itsvalue. Write one<option>for each suggestion. Inside a datalist, you may write the closing</option>or leave it out.
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:
<datalist>: suggestions, and visitors may type anything. Good for cities, hobbies or search words.<select>: a closed list, and visitors must pick one option. Good for clothing sizes or payment methods.
⚠️ Suggestions that don't show up, or don't behave as you expect
listandidspelled differently.list="city-list"andid="citylist"don't connect, so no suggestions appear.listset to the input's own id.listmust hold the datalist's id, not the input's id. The input'sidis still for the label.- Treating a datalist as a must-pick dropdown. Answers outside the list are still accepted. If the answer must come from the list, use
<select>. - The datalist written inside the input.
<input>has no closing tag, so the datalist is a separate tag, written after the input. - Suggestions look different everywhere. The browser draws the suggestion box, and each browser draws it in its own way. So the box can look different on a phone and on a laptop, and it is hard to change with CSS.
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>.