HTML Datalist
In this page:
<input list="listid">
<datalist id="listid">
<option value="value">
</datalist>
What Is <datalist>?
The <datalist> element defines a set of predefined <option> values that can be suggested to a visitor while they type into a connected text input, without restricting them to only those choices.
- Think of datalist suggestions as helpful shortcuts, not a strict set of allowed answers.
- Accepted attributes:
- datalist id — referenced by the input list attribute
- option value — suggested value
- option label — optional text
Example: What Is <datalist>?
<input list="browsers">
<datalist id="browsers">
<option value="Chrome">
<option value="Firefox">
</datalist>
Connecting a Datalist to an Input
The connection between an input and its suggestions relies entirely on the input's list attribute matching the datalist's id. Once linked, the browser automatically shows the datalist's options as the visitor types.
- Give the datalist a descriptive, unique id so it is easy to trace which input it belongs to in larger forms.
- Accepted values:
- list — the id of a datalist element
- Works with text, search, url, tel, email, number, date, color, range
Example: Connecting a Datalist to an Input
<input list="fruits" name="fruit">
<datalist id="fruits">
<option value="Apple">
<option value="Banana">
</datalist>
Populating Datalist Options Dynamically
Datalist options do not have to be fixed in the HTML source; they can be added or replaced with JavaScript, which is useful for suggestions that come from a search API or change based on visitor activity.
Example: Populating Datalist Options Dynamically
<input list="results" id="search">
<datalist id="results"></datalist>
<script>
document.getElementById('results').innerHTML = '<option value="Apple">';
</script>
Datalist vs Select Dropdown
A <select> dropdown restricts visitors to only the listed options, while a <datalist> merely suggests values and still accepts any free-typed text. Choosing between them depends on whether flexibility or strict validation matters more.
Example: Datalist vs Select Dropdown
<select>
<option value="Small">Small</option>
</select>
<input list="sizes">
<datalist id="sizes">
<option value="Small">
</datalist>
Browser Behavior and Limitations
While every modern browser supports <datalist>, the exact appearance of the suggestion dropdown, and how many options are shown at once, can differ slightly between browsers and devices.
Example: Browser Behavior and Limitations
<input list="colors">
<datalist id="colors">
<option value="Red">
<option value="Blue">
</datalist>
- Forgetting to connect the input and datalist with a matching list attribute and id, so no suggestions appear.
- Assuming a datalist restricts input to only the listed values, when visitors can still type anything they like.
- Populating a datalist with too many options, making the suggestion list overwhelming instead of helpful.
- The <datalist> element provides a list of suggested values for a connected text input.
- An input is connected to a datalist using a matching list attribute and the datalist's id.
- Unlike <select>, a datalist never restricts the visitor to only the suggested values.
The <datalist> element is supported by all modern browsers, though the exact suggestion appearance can vary slightly between them.
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- HTML Keyboard Shortcuts
- HTML Browser Support
- HTML Character Sets
- HTML Doctypes
- HTML Audio/Video Reference
- HTML Meta Tags
- HTML ARIA Roles
- HTML Input Validation
- HTML Iframe Security
- HTML Responsive Images
- HTML Web Fonts
- HTML Icon Fonts
- HTML Progress & Meter
- HTML Output Element
- HTML Datalist
- HTML Image Maps
- HTML Browser DevTools
- HTML Validation
- HTML Quiz
- HTML Interview Prep