Datalist for Autocomplete
Suggest input values with the datalist element.
Datalist for Autocomplete is a free HTML Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the HTML Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is datalist?
The <datalist> element provides a list of predefined suggestions for an <input>:
<label for="browser">Favorite browser</label>
<input list="browsers" id="browser" name="browser">
<datalist id="browsers">
<option value="Chrome">
<option value="Firefox">
<option value="Safari">
<option value="Edge">
</datalist>
<!-- list attribute links input to datalist by id -->
<!-- User can type freely OR select a suggestion -->datalist vs select
Key difference between <datalist> and <select>:
- datalist — free text + suggestions (user can type anything)
- select — constrained choice (user must pick from list)
Use datalist when you want to suggest values but not restrict input.
Linking datalist to input
The list attribute on <input> must match the id of the <datalist>:
<input type="text" list="fruits" name="fruit">
<datalist id="fruits">
<option value="Apple">
<option value="Banana">
<option value="Cherry">
</datalist>datalist with Different Input Types
<datalist> works with many input types:
<!-- text + datalist: type-ahead suggestions -->
<input type="text" list="countries">
<!-- range + datalist: tick marks -->
<input type="range" min="0" max="100" step="10" list="marks">
<datalist id="marks">
<option value="0"> <option value="25">
<option value="50"> <option value="75"> <option value="100">
</datalist>
<!-- color + datalist: color palette shortcuts -->
<input type="color" list="brand-colors">
<datalist id="brand-colors">
<option value="#0070f3">
<option value="#e86f00">
</datalist>option label in datalist
Options in datalist can have a visible label and a submitted value:
<datalist id="countries">
<option value="US">United States</option>
<option value="GB">United Kingdom</option>
<option value="DE">Germany</option>
</datalist>
<!-- In some browsers, label shows in dropdown
value is submitted when the option is selected -->Filtering Suggestions
The browser filters suggestions as the user types:
- The matching algorithm varies by browser
- Some match from the start; others match anywhere in the string
- You cannot control the browser's filtering behavior
- For custom filtering, build a custom autocomplete component with ARIA
Dynamic datalist with JavaScript
Update datalist options dynamically:
<input type="text" list="suggestions" id="search-input">
<datalist id="suggestions"></datalist>
<script>
document.getElementById('search-input').addEventListener('input', async function() {
const query = this.value;
const res = await fetch(`/api/search?q=${query}`);
const items = await res.json();
const dl = document.getElementById('suggestions');
dl.innerHTML = items.map(item =>
`<option value="${item.name}">`
).join('');
});
</script>datalist Accessibility
Accessibility considerations for datalist:
- Screen readers announce the suggestions as a listbox
- Users can navigate suggestions with arrow keys
- The input still accepts free text — make sure validation handles this
Browser Support Notes
Browser support for datalist is good but has quirks:
- Chrome, Edge, Firefox: full support
- Safari: supported from Safari 12.1+
- Mobile browsers: most show suggestions but UI varies
- Always test on target browsers before relying on datalist
When Not to Use datalist
Datalist is not ideal when:
- You need strict validation (user must pick from the list)
- You need custom option styling (browser controls all appearance)
- You need grouping (optgroup is not supported in datalist)
In those cases, use a custom autocomplete component with ARIA role="combobox".
datalist vs search input
Compare datalist vs search input with autocomplete:
<input type="search" name="q" list="search-suggestions">
<datalist id="search-suggestions">
<option value="HTML tutorial">
<option value="HTML forms">
<option value="HTML accessibility">
</datalist>
<!-- type="search" provides search-specific UI on mobile -->
<!-- list attribute adds datalist suggestions to any input type -->Quick Check
How do you link an input to its datalist element?
Recap: datalist
Datalist essentials:
<datalist id>+<input list>— linked by matching id/list- User can type freely or choose a suggestion
- Use
<select>when choice must be constrained - Works with text, range, color, and other input types
- Suggestions are filtered as the user types
Frequently asked questions
Is the “Datalist for Autocomplete” lesson free?
Yes — the full text of “Datalist for Autocomplete” is free to read here on the web, and the HTML Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the HTML Academy course, upgrade to CoddyKit PRO.
What will I learn in “Datalist for Autocomplete”?
Suggest input values with the datalist element. You practise HTML Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start HTML Academy?
No prior experience is required. HTML Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Datalist for Autocomplete” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this HTML Academy lesson?
Yes. Every HTML Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Select and option Dropdown Menus
- Textarea for Multi-line Input
- Checkbox and Radio Button Groups
- Datalist for Autocomplete