datalist

Provides autocomplete suggestions for input elements. The datalist creates a native dropdown of options that users can select from while still allowing free-form text entry.

When to Use

  • Providing suggestions while allowing custom values (e.g., country input)
  • Autocomplete functionality without JavaScript
  • Search inputs with common suggestions
  • When users might enter values not in the predefined list

Use select instead when users must choose from a fixed set of options.

Need more control? The combo-box web component offers keyboard navigation, filtering, custom rendering, and full styling control — a good upgrade path when native datalist falls short.

Basic Usage

Connect an input to a datalist using the list attribute on the input and an id on the datalist.

Code

Input Variants

Inputs with a list attribute display a dropdown arrow by default. Use .no-arrow to hide it, or .search for a search icon on the left.

Code

Options with Labels

Options can include a label displayed alongside the value. The value is what gets submitted, while the label provides additional context.

Code

With Different Input Types

Datalist works with various input types beyond text: email, URL, and range.

Using with form-field

For enhanced accessibility and validation, wrap datalist inputs in the form-field custom element.

Code

Recipes

These patterns extend the basic datalist with small amounts of JavaScript. The scripts live in the demo page, not in VB core — copy and adapt as needed.

Detect List Match vs Custom Entry

Check whether the user picked a value from the suggestion list or typed their own. Useful for conditional validation or analytics.

Dynamic Server-Driven Suggestions

Populate datalist options from an API endpoint with debounced input. The demo simulates a 150ms fetch; replace with a real endpoint in production.

Range with Visible Tick Labels

Datalist on a range input adds tick marks at each <option> value. Pair with visible labels and a live <output> for clear feedback.

Color Palette with Swatch Companion

A <input type="color" list="..."> shows palette swatches inside the native color picker on Chromium. The JS-rendered swatch buttons work cross-browser as a progressive enhancement companion.

Datalist vs combo-box

Choose based on the level of control you need:

Feature datalist combo-box
JavaScript required No Yes
Custom styling Browser-controlled Full control
Keyboard navigation Browser default ARIA combobox pattern
Custom filtering Browser default Configurable
Rich option content Text only HTML templates
Free-text entry Always allowed Configurable

Accessibility Notes

  • Always include a label: The input must have an associated label
  • Screen reader support: Native datalist is well-supported by assistive technologies
  • Keyboard navigation: Users can navigate options with arrow keys
  • Allow custom values: Unlike select, datalist allows users to enter values not in the list
  • Provide fallback: The input works without the datalist in older browsers

CSS Reference

Browser Support Notes

  • Datalist is supported in all modern browsers
  • Visual appearance varies between browsers (cannot be fully styled)
  • The dropdown behavior is controlled by the browser
  • On mobile devices, the suggestions may appear in a different format
  • type="color" with datalist shows palette swatches in Chromium browsers; Firefox and Safari fall back to the standard color picker

Related

  • input - The input element that uses datalist
  • option - Defines individual suggestions
  • select - For fixed option lists (no custom values)
  • label - Associates text with the input
  • form-field - Enhanced form field wrapper
  • combo-box - Full-featured combobox with custom styling and keyboard support