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 optgroup element puts headings inside a long dropdown

Html
~/tips/tip-html-optgroup-select

cat ~/tips/tip-html-optgroup-select.md

Group the choices of a select so people find theirs faster.

A select with forty entries in one flat list is tiring to scan. Grouping the choices takes a few extra tags and no script.

<label for="city">Office</label>
<select id="city" name="city">
  <optgroup label="Netherlands">
    <option value="lei">Leiden</option>
    <option value="utr">Utrecht</option>
  </optgroup>
  <optgroup label="Belgium" disabled>
    <option value="gnt">Gent</option>
  </optgroup>
</select>

The label shows as a heading that cannot be picked, with the options indented under it. On phones the native picker shows the groups too, in its own style.

The disabled attribute on a group greys out everything inside at once, which is handy when a whole region is not available yet.

One thing I keep in mind: the heading is not sent with the form. Only the chosen option’s value arrives at your server, so make values unique across groups. Do not try to nest one group in another for a second level, browsers do not support that reliably. And styling is limited because the system draws much of the dropdown itself. If you need search inside the list, that is a different control altogether.

Further reading: MDN: the optgroup element

More Html tips import { format } from "dates"; width="640" height="360" poster="demo.jpg"> srclang="en" label="English" default> An HTML noindex meta tag keeps one page out of search results title="Example blog" href="/feed.xml"> All Html tips (14) All topics

Link