HTML Datalist
In this page:
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.
Note: Think of datalist suggestions as helpful shortcuts, not a strict set of allowed answers.
Warning: A datalist with no connected input has no visible effect on the page at all.
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.
Note: Give the datalist a descriptive, unique id so it is easy to trace which input it belongs to in larger forms.
Warning: A mismatched list and id pair is a silent failure; no error appears, the suggestions simply never show.
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.
Note: Debounce network requests when building suggestions from an API, so you are not sending a request on every single keystroke.
Warning: Clearing and rebuilding a large number of options frequently can cause a noticeable flicker in the suggestion list.
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.
Note: Use <select> when only a fixed set of answers is valid, and <datalist> when guiding visitors is helpful but free text should still be accepted.
Warning: Using <datalist> when strict validation is required means invalid values must still be caught separately, since the datalist itself does not enforce them.
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.
Note: Test your datalist on a few different browsers to confirm the suggestion list looks and behaves the way you expect.
Warning: Some mobile browsers display datalist suggestions differently than desktop browsers, so always check the experience on a phone or tablet too.
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