HTML Beginner

Native autocomplete without JavaScript

Building autocomplete used to mean pulling in a library like Typeahead or Awesomplete and wiring up event listeners. The datalist element gives browsers native autocomplete with zero JavaScript.

Modern
4 lines
1<input list="countries" id="country" name="country" />
2<datalist id="countries">
3  <option>Afghanistan</option>
4  <option>Albania</option>
5</datalist>
Old JS library
1/* Add autocomplete library (Awesomplete, Typeahead, etc.) */
2import Awesomplete from 'awesomplete';
3new Awesomplete(document.querySelector('#country'), {
4  list: ['Afghanistan', 'Albania', 'Algeria', ...]
5});
6/* Plus custom CSS for the dropdown */
Limited availability 96% global usage

This feature is not Baseline because it does not work in some of the most widely-used browsers.

20+
12.1+
79+
type to see native autocomplete suggestions
<datalist> β€” no JavaScript, native browser autocomplete
Kinsta

Your first month is free

Managed WordPress hosting for faster sites.

Learn more
⚑

Zero JavaScript

No library, no event listeners, no custom dropdown. Link an input to a datalist with the list attribute and the browser does the rest.

✦

Free-text allowed

Unlike a select, datalist lets users type anything. The suggestions are hints, not restrictions. Useful for search fields and open-ended inputs.

∞

Works on any input type

datalist works on text, email, url, search, tel, and number inputs. It also adds tick marks to range inputs and swatches to color inputs.

JS Removed
0 lines
No library needed
Old Approach
JS libraries
Typeahead, Awesomplete
Modern Approach
<datalist>
Native browser autocomplete

How it works

The <datalist> element holds a list of <option> elements. Connect it to an input by matching the input's list attribute to the datalist's id. The browser shows matching suggestions as the user types and filters them automatically.

Options use the text content format: <option>Value</option>, not the value attribute. The browser renders the dropdown in its native UI. That means the position, size, and styling of the suggestion list is entirely browser-controlled and varies by OS. The input itself can be styled normally, but the dropdown cannot.

Unlike <select>, the user can type any value β€” the datalist is suggestive, not restrictive. This makes it good for search fields, tagging inputs, and anywhere you want to guide input without forcing a specific choice.

New CSS drops.

Join 600+ readers who've survived clearfix hacks.

ESC