HTML list Attribute

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 3 Examples
Forms & Input

Introduction

The list attribute is an HTML feature that connects an <input> element to a <datalist> element. It provides a predefined set of autocomplete suggestions while still letting users type their own value. This is not related to <ul> or <ol> bullet lists—those are separate elements with no list attribute.

What You’ll Learn

01

input + datalist

Linked by id.

02

id Reference

list="fruits".

03

Autocomplete

Suggest, not restrict.

04

list vs select

Free text vs fixed.

05

Not ul/ol

Common confusion.

06

JS list

Swap datalists.

Purpose of list

The primary purpose of the list attribute is to associate an <input> with a set of predefined options defined in a <datalist>. Users see matching suggestions as they type, which speeds up data entry and reduces typos on common values like countries, colors, or product names.

Unlike a <select> dropdown, the user is not limited to the suggested values—they can submit any text that fits the input’s validation rules.

⚠️
Not for <ul> or <ol>

The old meta description sometimes called this a “list styling” attribute for ordered and unordered lists. That is incorrect. Use <ul>, <ol>, and CSS for document lists; use list only on <input> with <datalist>.

📝 Syntax

Point the input’s list attribute at the id of a <datalist>:

list.html
<label for="fruit">Select a fruit:</label>
<input type="text" id="fruit" name="fruit" list="fruits">

<datalist id="fruits">
  <option value="Apple">
  <option value="Banana">
  <option value="Orange">
  <option value="Grapes">
  <option value="Strawberry">
</datalist>

Syntax Rules

  • Valid on <input> elements (text, search, url, tel, email, number, etc.).
  • Value is the id of a <datalist> in the document—no # prefix.
  • The datalist id must be unique and must match the list value exactly.
  • Each suggestion is an <option value="..."> inside the datalist.
  • Users can pick a suggestion or type a value not in the list (unless other validation blocks it).
  • In JavaScript, input.list returns the linked HTMLDataListElement.

💎 Values

The list attribute accepts a single token: the id of a <datalist> element.

  • list="fruits" — Links to <datalist id="fruits">.
  • list="countries" — Another datalist for a different input.
  • Invalid or missing id — No suggestions appear; input still works as plain text.

Update the association in JavaScript:

list-js.html
document.getElementById("dynamicInput").setAttribute("list", "vegetables");
// or: input.list = document.getElementById("vegetables");

⚡ Quick Reference

Use CaseMarkupNotes
Fruit suggestions<input list="fruits">+ <datalist id="fruits">
Email domain hintslist="domains"Works with type="email"
Strict dropdownUse <select>Not list
JavaScript linkinput.list = datalistElSwap suggestions
Label option text<option label="...">Display differs from value

Applicable Elements

Element / ContextSupported?Notes
<input>YesPrimary use case
<datalist>TargetReferenced by list id
<ul>, <ol>NoDocument lists, not this attribute
<select>NoUse options inside select instead

list + datalist vs <select>

Featureinput list + datalist<select>
User can type freelyYesNo (unless multiple + special cases)
Suggestions onlyYesFixed options
Native dropdown UIAutocomplete popupFull select menu
Best forKnown values + custom entryMust pick one option

Examples Gallery

Fruit autocomplete, switching between two datalists, and updating list with JavaScript.

👀 Live Preview

Click the field and type A or B to see suggestions:

The input uses list="demo-fruits" linked to the datalist below it in the DOM.

Example — Fruit Autocomplete

list.html
<label for="fruit">Select a Fruit:</label>
<input type="text" id="fruit" name="fruit" list="fruits">

<datalist id="fruits">
  <option value="Apple">
  <option value="Banana">
  <option value="Orange">
  <option value="Grapes">
  <option value="Strawberry">
</datalist>
Try It Yourself

How It Works

list="fruits" connects the input to <datalist id="fruits">. The id must match exactly.

Example — Switch Between Datalists

Use different suggestion sets for different contexts:

list-switch.html
<input id="food" list="fruits" placeholder="Pick a food">
<button type="button" id="useVeg">Use vegetables list</button>

<datalist id="fruits"></datalist>
<datalist id="vegetables"></datalist>

<script>
  document.getElementById("useVeg").onclick = function () {
    document.getElementById("food").setAttribute("list", "vegetables");
  };
</script>
Try It Yourself

How It Works

Changing the list attribute re-points the input to a different datalist without recreating the input element.

Dynamic Values with JavaScript

dynamic-list.html
function updateListAttribute(category) {
  var input = document.getElementById("dynamicInput");
  input.setAttribute("list", category === "veg" ? "vegetables" : "fruits");
}
Try It Yourself

How It Works

Dynamic updates keep one input field but swap suggestion sources based on user choices or API data.

♿ Accessibility

  • Always pair with <label> — Use for matching the input id so screen readers announce the field purpose.
  • Datalist support varies — Some assistive tech exposes suggestions inconsistently; do not rely on datalist alone for critical validation.
  • Validate server-side — Users can submit values outside the suggestion list.
  • Provide clear instructions — Hint text helps users know they can type or pick a suggestion.
  • Use select when choice must be restricted — For required fixed options, a select is more predictable for accessibility.

🧠 How list Works

1

Author adds list on input

Value = datalist id.

Markup
2

User focuses and types

Browser reads options.

Input
3

Matching suggestions shown

Filtered datalist values.

UI
=

Faster, flexible entry

Pick or type custom value.

Browser Support

The list attribute with <datalist> is supported in all modern browsers. Styling of the suggestion popup is limited and varies by platform.

HTML5 · Widely supported

Autocomplete suggestions in forms

Chrome, Firefox, Safari, and Edge support input list + datalist.

97% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
list + datalist 97% supported

Bottom line: Safe for progressive enhancement; validate submitted values on the server.

💡 Best Practices

✅ Do

  • Match list to a valid datalist id
  • Use labels and helpful placeholder text
  • Offer datalist for common values users might type anyway
  • Validate submitted data server-side
  • Use select when only fixed options are allowed

❌ Don’t

  • Confuse list with <ul> / <ol>
  • Rely on datalist as the only form of validation
  • Reference a datalist id that does not exist
  • Expect identical suggestion UI across all browsers
  • Use list on <select> (invalid)

Conclusion

The list attribute is a valuable tool for creating interactive, user-friendly forms in HTML. By associating <input> elements with <datalist> elements, you guide users through predefined options while keeping input flexible.

Remember: this attribute is about autocomplete suggestions, not bullet or numbered lists. Pair it with proper labels, validate on the server, and choose select when users must pick exactly one option.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about list

Bookmark these before adding autocomplete to forms.

5
Core concepts
🔍 02

Autocomplete

Suggest, not lock

Behavior
📄 03

Not ul/ol

Different feature

Compare
🔄 04

Swap lists

JS updates list

Dynamic
05

Validate

Server-side check

A11y

❓ Frequently Asked Questions

It links an <input> to a <datalist> by id so the browser shows autocomplete suggestions from the datalist options.
No. Bullet and numbered lists use <ul> and <ol> elements. The list attribute is only for <input> + <datalist>.
The id of a <datalist> element, without a hash—for example list="fruits" matches <datalist id="fruits">.
select restricts choices. input list suggests options but allows custom text the user types.
Yes. Use input.setAttribute("list", "newId") or assign input.list to a different datalist element.
No. Datalist provides hints only. Users can submit values not in the list unless you add separate validation.

Add smart autocomplete to your forms

Practice linking list on <input> to <datalist> options in the Try It editor.

Try fruit datalist example →

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.

5 people found this page helpful