🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEhtml

html Documentation

LOADING ENGINE...

<datalist>

AI & DATA SCIENCE // datalist-tag

The <datalist> element provides a set of predefined autocomplete suggestions for an associated <input>, while still allowing free-form entry.

Syntax

<input list="colors">
<datalist id="colors">
  <option value="Red"></option>
  <option value="Blue"></option>
</datalist>

Deep Dive Course

**`<datalist>`** connects to an `<input>` via the input's **`list`** attribute matching the datalist's `id`. Unlike `<select>`, a `<datalist>`-linked input still lets the user type ANY value — the listed options are just suggestions shown in an autocomplete-style dropdown, not a restriction. This makes it ideal for cases like 'suggest common answers, but allow a custom one too' (e.g. suggesting popular cities in a text field that still accepts any city name).

1Understanding <datalist>

`<datalist>` connects to an <input> via the input's `list` attribute matching the datalist's id. Unlike <select>, a <datalist>-linked input still lets the user type ANY value — the listed options are just suggestions shown in an autocomplete-style dropdown, not a restriction. This makes it ideal for cases like 'suggest common answers, but allow a custom one too' (e.g. suggesting popular cities in a text field that still accepts any city name).

💡

Unlike <select>, a <datalist> never restricts the input's actual value — always validate the submitted value server-side (or with the input's own pattern/type constraints) if it truly must be one of the suggested options.

editor.html
<label for="browser">Favorite browser</label>
<input list="browsers" id="browser" name="browser">
<datalist id="browsers">
  <option value="Chrome"></option>
  <option value="Firefox"></option>
  <option value="Safari"></option>
</datalist>
localhost:3000

2Practical Example

Here is a real-world application of <datalist> showing how it is used in production HTML.

editor.html
<!-- Suggesting common quantities while still allowing any number -->
<input type="number" list="quantities" name="qty">
<datalist id="quantities">
  <option value="1"></option>
  <option value="5"></option>
  <option value="10"></option>
</datalist>
localhost:3000

3Best Practices

Follow these guidelines when working with <datalist>:

1. Use <datalist> when you want to SUGGEST common values but still allow free-form input

2. Use <select> instead when the value truly must be restricted to a fixed set of choices

3. Give meaningful, unique values in each <option> — they populate the suggestion dropdown text

⚠️

Tip: Unlike <select>, a <datalist> never restricts the input's actual value — always validate the submitted value server-side (or with the input's own pattern/type constraints) if it truly must be one of the suggested options.

editor.html
<label for="browser">Favorite browser</label>
<input list="browsers" id="browser" name="browser">
<datalist id="browsers">
  <option value="Chrome"></option>
  <option value="Firefox"></option>
  <option value="Safari"></option>
</datalist>
localhost:3000

Examples

Example 01Basic Usage
<label for="browser">Favorite browser</label>
<input list="browsers" id="browser" name="browser">
<datalist id="browsers">
  <option value="Chrome"></option>
  <option value="Firefox"></option>
  <option value="Safari"></option>
</datalist>
Example 02Advanced Example
<!-- Suggesting common quantities while still allowing any number -->
<input type="number" list="quantities" name="qty">
<datalist id="quantities">
  <option value="1"></option>
  <option value="5"></option>
  <option value="10"></option>
</datalist>

Best Practices

  • Use when you want to SUGGEST common values but still allow free-form input
  • Use restricts the user to only the options you've defined — there's no way to submit a value that isn't one of the listed

Our Commitment to Quality

Our mission is to provide accessible, high-quality web development education. All tutorials are crafted by seasoned industry professionals and rigorously reviewed for accuracy. We base our content on official documentation from sources like the Mozilla Developer Network (MDN) and W3C standards to ensure you learn the most current and correct information.

© 2025 Code Syllabus. All rights reserved.Privacy PolicyData ProtectionTerms of Use