The <datalist> Element for Autocomplete
You'll learn how to provide helpful autocomplete suggestions to users as they type into a text input.
What is <datalist>?
The <datalist> element creates a predefined list of suggested values that appear as a dropdown when users interact with an associated text input. Think of it like your phone's keyboard suggesting words as you type—you can accept a suggestion or keep typing your own text.
How It Works
You connect a <datalist> to an <input> using matching id and list attributes. Inside the <datalist>, you add <option> elements with suggested values:
<label for="browser">Choose your browser:</label>
<input type="text" id="browser" list="browsers" name="browser">
<datalist id="browsers">
<option value="Chrome">
<option value="Firefox">
<option value="Safari">
<option value="Edge">
</datalist>
When users click or start typing in the input, they'll see the list of browsers. If they type "F", the list filters to show only "Firefox". They can either select from the list or type something completely different—unlike <select>, the <datalist> merely suggests, it doesn't restrict.
Real-World Use
Use <datalist> for fields where users might type common values but need flexibility: city names, product categories, or common search terms. It combines the freedom of text input with the convenience of a dropdown.
Key Takeaway: The <datalist> element provides autocomplete suggestions for text inputs by linking predefined <option> values through matching list and id attributes, helping users input data faster while maintaining typing freedom.