HTML <datalist> Tag

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 4 Examples
Forms & Controls

What You’ll Learn

By the end of this tutorial, you’ll add autocomplete suggestions to inputs with <datalist> and know when to use it instead of <select>.

01

Datalist Syntax

Connect input list to datalist id.

02

option Children

Define suggestions with <option value>.

03

Free Text Input

Users can pick a suggestion or type their own value.

04

Dynamic Options

Update suggestions with JavaScript as users type.

05

vs select & data

Distinguish datalist from fixed dropdowns and the data element.

06

Accessibility

Label inputs, validate server-side, choose the right control.

What Is the <datalist> Tag?

The datalist element (<datalist>) contains a list of <option> values that browsers suggest when the user focuses or types in a linked input. It is not visible on its own—it powers the input’s suggestion UI.

💡
Suggestions, not restrictions

Unlike <select>, datalist lets users type values not in the list. Validate submitted data server-side when only certain values are allowed.

Common uses include browser names, countries, cities, product search, email domain hints, and any field where suggestions help but custom input is still allowed.

📝 Syntax

Give the <datalist> an id, then reference it from the input’s list attribute:

syntax.html
<label for="browser">Browser:</label>
<input list="browser-list" id="browser" name="browser">

<datalist id="browser-list">
  <option value="Chrome">
  <option value="Firefox">
  <option value="Safari">
</datalist>

Syntax Rules

  • The list attribute goes on the input, not on datalist.
  • input list must match the datalist id exactly.
  • Each suggestion is an <option value="..."> inside the datalist.
  • Works with input types like text, search, url, email, and number.

⚡ Quick Reference

FeatureSyntax / RuleNotes
On inputlist="id"Points to datalist id
On datalistid="..."Target for list attribute
Children<option value>Each suggestion
User inputFree textNot limited to listed values
Not the samedataMachine-readable value element
vs selectSuggestionsselect = fixed choices only

⚖️ <datalist> vs <select>

Choose the right control for your form field:

Feature<datalist><select>
BehaviorSuggests valuesFixed dropdown list
Custom inputYes — user can type anythingNo — must pick an option
Best forAutocomplete, search boxesStrict choice lists
ValidationServer-side recommendedBrowser enforces options

🧰 Attributes

The list attribute goes on the input. The datalist uses id and contains option children:

list On input

References the id of a datalist element.

list="fruits"
id On datalist

Target matched by the input’s list attribute.

id="fruits"
value On option

The suggestion text shown to the user.

value="Apple"
label On option

Optional display label when different from value.

label="Apple Inc."
class Global

Style the linked input—datalist itself is not visible.

class="autocomplete"
name On input

Form submission uses the input’s name and typed value.

name="fruit"

Example: <input list="fruits" id="fruit-input" name="fruit"> + <datalist id="fruits">...</datalist>

Examples Gallery

Basic autocomplete, browser picker, dynamic JavaScript options, and datalist vs select with copy-ready code and live previews.

Live Preview

Click the input and type to see browser suggestions (behavior varies slightly by browser):

Basic Autocomplete

A simple input linked to a datalist of predefined options.

basic-datalist.html
<label for="choice">Choose an option:</label>
<input list="options-list" id="choice" name="choice">
<datalist id="options-list">
  <option value="Option 1">
  <option value="Option 2">
  <option value="Option 3">
</datalist>
Try It Yourself

📚 Common Use Cases

Use <datalist> for browser names, countries, cities, product search, email domain hints, and any field where suggestions help but custom input is still allowed.

Browser Picker

Suggest common browser names while the user can still type a different one.

browser-datalist.html
<label for="browsers">Choose a browser:</label>
<input list="browser-list" id="browsers" name="browsers">
<datalist id="browser-list">
  <option value="Chrome">
  <option value="Firefox">
  <option value="Safari">
  <option value="Edge">
</datalist>
Try It Yourself

Dynamic Options with JavaScript

Filter and add option elements inside datalist as the user types.

dynamic-datalist.html
<input list="country-list" id="countries">
<datalist id="country-list"></datalist>

<script>
  var countries = ["India", "Indonesia", "Italy", "Iceland"];
  var input = document.getElementById("countries");
  var list = document.getElementById("country-list");

  input.addEventListener("input", function () {
    var q = input.value.toLowerCase();
    list.innerHTML = "";
    countries.forEach(function (c) {
      if (q && c.toLowerCase().startsWith(q)) {
        var opt = document.createElement("option");
        opt.value = c;
        list.appendChild(opt);
      }
    });
  });
</script>
Try It Yourself

datalist vs select

Compare flexible suggestions with a strict dropdown.

datalist-vs-select.html
<!-- datalist: custom input allowed -->
<input list="colors" name="color-dl">
<datalist id="colors">
  <option value="Red"><option value="Green">
</datalist>

<!-- select: fixed choices only -->
<select name="color-sel">
  <option>Red</option><option>Green</option>
</select>
Try It Yourself

Styling <datalist> Inputs with CSS

The datalist itself is invisible—style the linked input and its label for a polished autocomplete field:

border Input outline
padding Comfortable tap target
:focus-visible Keyboard focus ring
max-width Constrain field width
datalist-input.css
/* Style the linked input, not datalist */
.autocomplete-field {
  display: block;
  width: 100%;
  max-width: 280px;
  padding: 0.5rem 0.75rem;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  font-size: 0.9375rem;
}

.autocomplete-field:focus-visible {
  outline: 2px solid #2563eb;
  outline-offset: 2px;
}

Live styled autocomplete input

♿ Accessibility

Make datalist-enhanced inputs usable for everyone:

  • Always use label — associate it with the input, not the datalist.
  • Use select when choices are strict — better screen reader support for fixed lists.
  • Validate server-side — users can submit values not in the datalist.
  • Keep suggestions relevant — short, clear option values help all users.

🧠 How <datalist> Works

1

Author defines options in datalist

Static option elements or dynamic JS updates.

Markup
2

Input links via list attribute

list on input matches id on datalist.

Connection
3

Browser shows suggestions

User picks a suggestion or types a custom value.

UX
=

Faster, friendlier forms

Autocomplete helps users without locking them into fixed choices.

Modern Browser Support

The <datalist> element is fully supported in all modern browsers. Internet Explorer 10+ has partial support with limited UI behavior.

HTML5 · Autocomplete

Input suggestions in every modern browser

Chrome, Firefox, Safari, Edge, and Opera all support datalist-linked inputs. Legacy IE has partial support with simpler UI.

98% Modern browser support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 10+ · Limited UI behavior
Partial support
Opera All modern versions
Full support
<datalist> tag 98% supported

Bottom line: Safe for all modern production environments. Test autocomplete UX in target browsers—UI details vary slightly between engines.

Conclusion

The <datalist> tag adds autocomplete suggestions to inputs through the list and id connection. Use it when suggestions help but free text is still valid—and choose <select> when users must pick from a fixed list only.

💡 Best Practices

✅ Do

  • Match input list to datalist id
  • Label the input clearly
  • Keep option lists concise and relevant
  • Validate submitted values on the server

❌ Don’t

  • Confuse datalist with data element
  • Put list on datalist (it goes on input)
  • Use datalist when only fixed choices are allowed
  • Assume users will only pick listed values

Key Takeaways

Knowledge Unlocked

Six truths every developer should know about <datalist>

Bookmark these before you ship — they’ll keep your autocomplete forms flexible and correct.

6
Core concepts
🔗 02

list + id

Connect with input list matching datalist id.

Essential
03

Free Text

Users can type values not in the suggestion list.

Behavior
04

Not data or select

Different from data element and fixed select dropdowns.

Reference
05

Dynamic JS

Add or remove option elements at runtime.

Scripting
🔒 06

Validate Server-Side

Never assume submitted values match the datalist options.

Security

❓ Frequently Asked Questions

It provides autocomplete suggestions for an input field, linked through the input’s list attribute.
datalist suggests values but allows free text. select restricts the user to listed options only.
Set datalist id="my-list" and input list="my-list".
No. Completely different elements with different purposes.
Yes. Add or remove option elements inside the datalist dynamically.

Build Autocomplete Inputs

Practice list, datalist, and option in the interactive editor.

Try datalist →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful