More HTML answers

by Danny
Published 2 min read

The datalist element gives any text input a list of suggestions with no JavaScript. Here is how to use it, which input types it works with, and where its limits are.

The short answer

#

Create a datalist with an id, fill it with option elements, and point the input's list attribute at that id. The browser shows matching suggestions as the user types, but they can still type anything.


_10
<label for="browser">Favourite browser</label>
_10
<input id="browser" name="browser" list="browsers">
_10
_10
<datalist id="browsers">
_10
<option value="Chrome"></option>
_10
<option value="Edge"></option>
_10
<option value="Firefox"></option>
_10
<option value="Safari"></option>
_10
</datalist>

Why it happens

#

Developers often reach for a JavaScript autocomplete library when they only need a short list of suggestions. HTML has had a built-in option for years: datalist. It works like a combo box, a text input with a dropdown of suggestions, and the browser handles filtering, keyboard navigation and the popup for you.

The key thing to understand is that datalist suggests, it does not restrict. It is not a replacement for select. Whatever the user types is what gets submitted.

How to fix it

#

Basic suggestions

The example in the short answer is all you need for a simple list. The browser filters options as the user types. The datalist itself is not rendered on the page, so you can put it anywhere in the document, though keeping it next to the input makes the code easier to follow.

Always keep a visible label. The datalist does not label the input.

Use it with other input types

datalist works with many input types, not just text. The browser shows the suggestions in a way that fits the control.


_13
<label for="email">Email</label>
_13
<input id="email" type="email" list="domains">
_13
<datalist id="domains">
_13
<option value="[email protected]"></option>
_13
</datalist>
_13
_13
<label for="volume">Volume</label>
_13
<input id="volume" type="range" min="0" max="100" list="marks">
_13
<datalist id="marks">
_13
<option value="0"></option>
_13
<option value="50"></option>
_13
<option value="100"></option>
_13
</datalist>

On a range input, some browsers draw tick marks at the listed values. On colour and date inputs, support and appearance vary a lot between browsers, so treat it as a bonus there.

Restrict the value when you need to

If only listed values are valid, use a select instead. If you want the typing experience of datalist but must limit the value, add a pattern built from the allowed values, and validate on the server as well.


_10
<input id="size" name="size" list="sizes" pattern="Small|Medium|Large" required>
_10
<datalist id="sizes">
_10
<option value="Small"></option>
_10
<option value="Medium"></option>
_10
<option value="Large"></option>
_10
</datalist>

Add suggestions from the server

Because it is plain HTML, you can render the options on the server, for example a list of existing tags or recent searches. If the list must change as the user types, you will need a little JavaScript to fetch new options and replace the option elements.

Common pitfalls

#
  • You cannot style the dropdown. The popup is drawn by the browser and ignores your CSS. If the design needs custom styling, rich option content or icons, you need a custom component.
  • Labels on options behave differently. Some browsers show an option's text label next to its value, others show only the value. Do not rely on the label to explain the value.
  • Filtering is the browser's choice. Some match from the start of the word, others match anywhere. You cannot change it.
  • Mobile support is uneven. Some mobile browsers show suggestions above the keyboard, and some have had patchy support. Make sure the input works fine with no suggestions at all.
  • Browser autofill can compete. Saved form entries may appear alongside or instead of your suggestions. Set a suitable autocomplete value, or autocomplete="off" if autofill is not useful for that field.
  • Very long lists. Thousands of options make the page heavy and the popup hard to use. For large data sets, filter on the server.
  • Expecting a select. Users can submit any text, so always validate the value.
#

Was this resource helpful?