Coding Hubs School of AI – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 8448811540
Back to HTML Notes
Topic #721

datalist

The <datalist> element attaches a list of suggested autocomplete values to a text input — while still letting the user type any value they want, not just the suggestions.

Syntax

<input type="text" list="suggestions">
<datalist id="suggestions">
    <option value="Suggestion One">
    <option value="Suggestion Two">
</datalist>

Basic Example

<label for="course-search">Search a course</label>
<input type="text" id="course-search" name="course-search" list="course-options">
<datalist id="course-options">
    <option value="Data Science">
    <option value="Full Stack Development">
    <option value="Cloud & DevOps">
</datalist>

Browser output: as the user starts typing, a native dropdown of matching suggestions appears beneath the field — but unlike <select>, they can still submit any freely typed value, not just one of the listed suggestions.

How It Works

The list attribute on the <input> connects it to a <datalist>'s id — matching the same for/id-style pairing pattern used elsewhere in HTML.

Important Attributes

AttributePurpose
listOn the input — connects it to a <datalist>'s id
idOn the datalist — the target the input's list points to

Comparison: select vs datalist

<select><datalist> + <input>
Free typing allowed?No — must pick from the listYes — suggestions are optional hints, not a hard constraint
Use when...Only a fixed set of values is genuinely validCommon values exist, but custom input should still be accepted

Real-World Example

<label for="city">City</label>
<input type="text" id="city" name="city" list="cities">
<datalist id="cities">
    <option value="Delhi">
    <option value="Noida">
    <option value="Gurugram">
</datalist>

A city field that suggests common options but still accepts any city the user actually types — exactly the flexible middle ground <datalist> is built for.

Common Mistakes

  • Assuming <datalist> restricts input the way <select> does — it never does; any typed value is still valid and will be submitted
  • Forgetting the list/id connection, leaving the datalist orphaned and having no effect at all

Best Practices

  • Use <datalist> when common suggestions exist but the field shouldn't be strictly limited to them
  • Use <select> instead when only a fixed, closed set of values is genuinely valid

Accessibility Considerations

Support quality for datalist suggestions varies somewhat across screen readers — always ensure the field remains fully usable by plain typing alone, since that's the guaranteed, universal fallback.

SEO Considerations

No direct ranking effect.

Browser & Modern HTML Notes

Well-supported in modern browsers, though the exact visual styling of the suggestion dropdown is largely outside CSS's control, similar to <select>.

Mini Practice

  1. Build a text input with a datalist of 4 suggested values.
  2. Explain, in your own words, why datalist suggestions don't restrict what a user can actually submit.

Related HTML Notes

Want to go beyond the notes?

Join Coding Hubs School of AI's HTML course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →