HTML Datalist

Data List in HTML Forms: Suggestions Without Restrictions

Imagine giving users a helpful nudge — a list of suggestions they can pick from — while still letting them type their own answer. That’s exactly what <datalist> offers: a bridge between control and freedom.

What is a Datalist?

<datalist> is an HTML element that provides a set of pre-defined options to an <input>. As users type, matching suggestions appear, but they’re not locked into the list — they can type anything they want.

Basic Syntax

<input list="fruits" name="fruit">
<datalist id="fruits">
  <option value="Apple">
  <option value="Banana">
  <option value="Cherry">
</datalist>
[ A ▼ ] — Suggestions: Apple, Banana, Cherry

When the user starts typing, options from the list appear as suggestions. It’s smart, flexible, and works beautifully for search bars, forms, and custom inputs.

Connecting <input> and <datalist>

The list attribute on the input field must match the id of the <datalist> element.

<input type="text" list="items" name="itemName">
<datalist id="items">
  <option value="Item 1">
  <option value="Item 2">
  <option value="Item 3">
</datalist>

This setup enables autocomplete-style interaction without enforcing the choices.

Customizing the Suggestion List

You can add as many <option> values as needed — even dynamically via JavaScript in advanced use cases (though that goes beyond HTML).

Placeholder + Datalist

Pair placeholder with datalist for a more guided UX.

<input type="text" name="fruitName" list="fruitList" placeholder="Choose or type a fruit">
<datalist id="fruitList">
  <option value="Apple">
  <option value="Banana">
  <option value="Cherry">
</datalist>
[ Choose or type a fruit ▼ ]

Real-World Example: Fruit Feedback with Suggestions

<form action="/submit-fruit" method="post">

  <label for="fruit">Your favorite fruit:</label><br>
  <input type="text" id="fruit" name="fruit" list="fruitOptions" placeholder="Apple, Banana, etc." required>
  
  <datalist id="fruitOptions">
    <option value="Apple">
    <option value="Banana">
    <option value="Cherry">
    <option value="Mango">
    <option value="Pineapple">
  </datalist><br><br>

  <input type="submit" value="Submit">
</form>
Your favorite fruit: [ type or pick ▼ ]  
[Submit]

This form gently suggests a few fruits but still respects the user’s freedom to enter something unique — like "Blueberry".

Browser Support

<datalist> is supported in all major browsers except Safari on iOS (which may ignore the suggestion dropdown but still allows typed values).

Best Practices

  • Use when there’s a long list of common answers (e.g., cities, tags, categories)
  • Don’t rely on datalist for strict validation — users can enter anything
  • Combine with required if the field must be filled
  • Use helpful placeholders to guide users on what to expect

Summary

The <datalist> element gives you the best of both worlds: suggested options and open input. You’ve now learned to:

  • Create a datalist element and connect it to an input
  • Offer relevant suggestions without limiting user input
  • Enhance form usability and speed without JavaScript

Check your understanding 1

Which HTML element provides a list of suggested options for an input field?
Choose one answer. You can try again.

Answer and explanation

<code><datalist></code>

The <datalist> tag is used to define a list of pre-defined options for an <input> element. It offers suggestions as the user types.

Check your understanding 2

A datalist allows users to only pick from the options it provides.
Choose one answer. You can try again.

Answer and explanation

False

A <datalist> suggests values, but does not restrict input. Users can still type in any value not in the list.

Check your understanding 3

Choose the option that lists all and only the correct statements.

  1. Suggesting cities for 'Place of Birth'
  2. Providing subject options for elective selection
  3. Allowing multi-select of sports activities
  4. Listing blood groups for student health info
Which of the following are valid use cases for using <datalist> in a school admission form?
Choose one answer. You can try again.

Answer and explanation

Statements 1 and 2 and 4

Datalist is ideal for providing autocomplete suggestions for single-value inputs, such as city names, subject lists, or standard dropdown-like fields like blood group. It does not support multi-select.

Check your understanding 4

How do you associate a <datalist> with an <input>?
Choose one answer. You can try again.

Answer and explanation

Using the <code>id</code> of the datalist in the input's <code>list</code> attribute

You connect a datalist to an input field by matching the input’s list attribute to the datalist’s id.

Check your understanding 5

You must use the <option> tag within a <datalist> element.
Choose one answer. You can try again.

Answer and explanation

True

Yes, each suggestion inside a datalist must be wrapped in an <option> tag for it to appear in the dropdown list.

Check your understanding 6

Choose the option that lists all and only the correct statements.

  1. <input list="cities">
    <datalist id="cities">
      <option value="Hyderabad">
      <option value="Chennai">
    </datalist>
  2. <input list="subjects">
    <list id="subjects">
      <option value="Maths">
    </list>
  3. <input type="text">
    <datalist>
      <option value="English">
    </datalist>
  4. <input list="bloodGroups">
    <datalist id="bloodGroups">
      <option value="B+">
      <option value="O-">
    </datalist>
Which of the following code snippets correctly implement a datalist?
Choose one answer. You can try again.

Answer and explanation

Statements 1 and 4

Only snippets using a valid <datalist> element with a matching list attribute in the input are correct. Option tags should be inside a proper datalist, not <list>.

What’s Next?

Up next: we explore the autofocus and autocomplete attributes — making your forms more intuitive from the moment the page loads.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: HTML Autofocus and Autocomplete →