brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: the HTML datalist element adds autocomplete suggestions to an input

Html
~/tips/tip-html-datalist-input-suggestions

cat ~/tips/tip-html-datalist-input-suggestions.md

Gives a text input a list of suggested values without JavaScript, while still allowing free typing.

Sometimes you want suggestions but not a strict dropdown. The datalist element does that in plain HTML, with no script.

<label for="flavor">Choose a flavor:</label>
<input list="flavors" id="flavor" name="flavor">

<datalist id="flavors">
  <option value="Chocolate"></option>
  <option value="Mint"></option>
  <option value="Vanilla"></option>
</datalist>

Give the datalist an id and point the input’s list attribute at it. The browser shows the options as suggestions while the user types, and picking one fills in its value.

It works for text-like types such as text, search, url, tel, email and number. It can also offer values for date and time inputs, tick marks on a range, and colors for a color input.

The caveat is that it is not a replacement for <select>. The user can still type anything that passes validation, so check the value on the server. MDN also lists accessibility problems: the option text does not scale with page zoom, the options can hardly be styled, and some screen reader and browser pairs, such as NVDA with Firefox, do not announce the popup. The feature is also not Baseline, so look at the compatibility table on MDN before depending on it.

Further reading: MDN: datalist

More Html tips import { format } from "dates"; width="640" height="360" poster="demo.jpg"> The HTML optgroup element puts headings inside a long dropdown srclang="en" label="English" default> An HTML noindex meta tag keeps one page out of search results All Html tips (14) All topics

Link