7.5 · Forms & UI
One open at a time: exclusive accordions
Give details elements the same name and only one can be open.
<details name="faq"> groups every details element
with that name. Opening one closes the others, the way radio buttons
work.
It’s HTML, so it works with the keyboard and with find-in-page, which
opens the matching section. Combined with
::details-content from the interpolate-size lesson, it
animates too.
details.forEach((d) =>
d.addEventListener('toggle', () => {
if (!d.open) return
details.forEach((o) => o !== d && (o.open = false))
}))
<details name="faq" open>…</details>
<details name="faq">…</details>
<details name="faq">…</details>