Live Preview
Click the input and type to see browser suggestions (behavior varies slightly by browser):

By the end of this tutorial, you’ll add autocomplete suggestions to inputs with <datalist> and know when to use it instead of <select>.
Connect input list to datalist id.
Define suggestions with <option value>.
Users can pick a suggestion or type their own value.
Update suggestions with JavaScript as users type.
Distinguish datalist from fixed dropdowns and the data element.
Label inputs, validate server-side, choose the right control.
<datalist> Tag?The datalist element (<datalist>) contains a list of <option> values that browsers suggest when the user focuses or types in a linked input. It is not visible on its own—it powers the input’s suggestion UI.
Unlike <select>, datalist lets users type values not in the list. Validate submitted data server-side when only certain values are allowed.
Common uses include browser names, countries, cities, product search, email domain hints, and any field where suggestions help but custom input is still allowed.
Give the <datalist> an id, then reference it from the input’s list attribute:
<label for="browser">Browser:</label>
<input list="browser-list" id="browser" name="browser">
<datalist id="browser-list">
<option value="Chrome">
<option value="Firefox">
<option value="Safari">
</datalist>list attribute goes on the input, not on datalist.input list must match the datalist id exactly.<option value="..."> inside the datalist.input types like text, search, url, email, and number.| Feature | Syntax / Rule | Notes |
|---|---|---|
| On input | list="id" | Points to datalist id |
| On datalist | id="..." | Target for list attribute |
| Children | <option value> | Each suggestion |
| User input | Free text | Not limited to listed values |
| Not the same | data | Machine-readable value element |
| vs select | Suggestions | select = fixed choices only |
<datalist> vs <select>Choose the right control for your form field:
| Feature | <datalist> | <select> |
|---|---|---|
| Behavior | Suggests values | Fixed dropdown list |
| Custom input | Yes — user can type anything | No — must pick an option |
| Best for | Autocomplete, search boxes | Strict choice lists |
| Validation | Server-side recommended | Browser enforces options |
The list attribute goes on the input. The datalist uses id and contains option children:
list On inputReferences the id of a datalist element.
list="fruits"id On datalistTarget matched by the input’s list attribute.
id="fruits"value On optionThe suggestion text shown to the user.
value="Apple"label On optionOptional display label when different from value.
label="Apple Inc."class GlobalStyle the linked input—datalist itself is not visible.
class="autocomplete"name On inputForm submission uses the input’s name and typed value.
name="fruit"Example: <input list="fruits" id="fruit-input" name="fruit"> + <datalist id="fruits">...</datalist>
Basic autocomplete, browser picker, dynamic JavaScript options, and datalist vs select with copy-ready code and live previews.
Click the input and type to see browser suggestions (behavior varies slightly by browser):
A simple input linked to a datalist of predefined options.
<label for="choice">Choose an option:</label>
<input list="options-list" id="choice" name="choice">
<datalist id="options-list">
<option value="Option 1">
<option value="Option 2">
<option value="Option 3">
</datalist>Use <datalist> for browser names, countries, cities, product search, email domain hints, and any field where suggestions help but custom input is still allowed.
Suggest common browser names while the user can still type a different one.
<label for="browsers">Choose a browser:</label>
<input list="browser-list" id="browsers" name="browsers">
<datalist id="browser-list">
<option value="Chrome">
<option value="Firefox">
<option value="Safari">
<option value="Edge">
</datalist>Filter and add option elements inside datalist as the user types.
<input list="country-list" id="countries">
<datalist id="country-list"></datalist>
<script>
var countries = ["India", "Indonesia", "Italy", "Iceland"];
var input = document.getElementById("countries");
var list = document.getElementById("country-list");
input.addEventListener("input", function () {
var q = input.value.toLowerCase();
list.innerHTML = "";
countries.forEach(function (c) {
if (q && c.toLowerCase().startsWith(q)) {
var opt = document.createElement("option");
opt.value = c;
list.appendChild(opt);
}
});
});
</script>Compare flexible suggestions with a strict dropdown.
<!-- datalist: custom input allowed -->
<input list="colors" name="color-dl">
<datalist id="colors">
<option value="Red"><option value="Green">
</datalist>
<!-- select: fixed choices only -->
<select name="color-sel">
<option>Red</option><option>Green</option>
</select>The datalist itself is invisible—style the linked input and its label for a polished autocomplete field:
border Input outlinepadding Comfortable tap target:focus-visible Keyboard focus ringmax-width Constrain field width/* Style the linked input, not datalist */
.autocomplete-field {
display: block;
width: 100%;
max-width: 280px;
padding: 0.5rem 0.75rem;
border: 1px solid #cbd5e1;
border-radius: 8px;
font-size: 0.9375rem;
}
.autocomplete-field:focus-visible {
outline: 2px solid #2563eb;
outline-offset: 2px;
}Live styled autocomplete input
Make datalist-enhanced inputs usable for everyone:
label — associate it with the input, not the datalist.select when choices are strict — better screen reader support for fixed lists.Static option elements or dynamic JS updates.
list on input matches id on datalist.
User picks a suggestion or types a custom value.
Autocomplete helps users without locking them into fixed choices.
The <datalist> element is fully supported in all modern browsers. Internet Explorer 10+ has partial support with limited UI behavior.
Chrome, Firefox, Safari, Edge, and Opera all support datalist-linked inputs. Legacy IE has partial support with simpler UI.
Bottom line: Safe for all modern production environments. Test autocomplete UX in target browsers—UI details vary slightly between engines.
The <datalist> tag adds autocomplete suggestions to inputs through the list and id connection. Use it when suggestions help but free text is still valid—and choose <select> when users must pick from a fixed list only.
input list to datalist iddatalist with data elementlist on datalist (it goes on input)<datalist>Bookmark these before you ship — they’ll keep your autocomplete forms flexible and correct.
<datalist> provides autocomplete options for linked inputs.
Connect with input list matching datalist id.
Users can type values not in the suggestion list.
BehaviorDifferent from data element and fixed select dropdowns.
Add or remove option elements at runtime.
Never assume submitted values match the datalist options.
Securitylist attribute.datalist suggests values but allows free text. select restricts the user to listed options only.datalist id="my-list" and input list="my-list".option elements inside the datalist dynamically.Practice list, datalist, and option in the interactive editor.
6 people found this page helpful