Sub topic 8
Exclusive accordions with the name attribute
This chapter assumes you can write basic HTML and CSS, and that you can select elements and add event listeners in JavaScript. It also assumes you know how details and summary work and how the open attribute sets the starting state. Here you will group several details elements so that opening one closes the others.
The problem with independent widgets
Each details element keeps its own open state. If you place three of them on a page, a visitor can open all three at once. For an FAQ this is often fine. For a settings panel or a list of steps, you may want only one section open at a time. That layout is called an exclusive accordion.
Before the name attribute existed, this needed JavaScript. You listened for toggle events and closed the siblings by hand. Current browsers now do it natively.
Grouping with the name attribute
Give several details elements the same name value. The browser treats them as one group and allows at most one member to be open.
<details name="faq">
<summary>How do I reset my password?</summary>
<p>Use the reset link on the sign-in page.</p>
</details>
<details name="faq">
<summary>How do I change my email?</summary>
<p>Open account settings and edit the email field.</p>
</details>
<details name="faq">
<summary>How do I delete my account?</summary>
<p>Contact support from the help page.</p>
</details>When the visitor opens the second item, the browser removes the open attribute from whichever group member was open. Opening the first item later closes the second. The visitor can still close the open item and leave all of them closed.
The name attribute here works like the name on radio buttons. The shared value defines the group. Unlike radio buttons, a group of details elements can have zero items open.
What happens to the open attribute
The browser manages open for you. The group follows these rules:
- Opening a member closes any other open member of the same group.
- Closing a member does not open another.
- Each closed member fires a
toggleevent, as does the member that opened.
This matters if you use the toggle event for tracking or lazy loading. A single click can produce two events, one for the item that closed and one for the item that opened. Check event.newState or details.open inside the handler rather than assuming each event means an opening.
Initial state in a group
You can still use the open attribute to choose which item starts expanded.
<details name="steps" open>
<summary>Step 1: Install</summary>
<p>Download the package and run the installer.</p>
</details>
<details name="steps">
<summary>Step 2: Configure</summary>
<p>Edit the configuration file.</p>
</details>Avoid marking more than one member of a group as open in your HTML. The specification says the browser keeps only the first one open when the document loads and closes the rest. Setting two is a markup mistake, and the result can surprise a reader of your source.
Reading the group from JavaScript
You can select a group with an attribute selector and find the open member.
const group = document.querySelectorAll('details[name="faq"]');
function currentlyOpen() {
return [...group].find((item) => item.open) ?? null;
}
group.forEach((item) => {
item.addEventListener('toggle', () => {
if (item.open) {
console.log('Opened:', item.querySelector('summary').textContent);
}
});
});The if (item.open) check ignores the closing events, so you log once per user action. Setting item.open = true in code follows the same grouping rules, so a script cannot open two members of one group either.
Styling and scope
The styles from the earlier chapters apply unchanged. The selector details[open] matches whichever member is expanded, so you can highlight the active section with a single rule.
details[name="faq"] {
border: 1px solid #ccc;
padding: 0.5rem 1rem;
}
details[name="faq"][open] {
border-color: royalblue;
}Gotchas
- Group scope. Elements share a group only when they are in the same tree, such as the same document or the same shadow root. Using the same name in two different shadow roots creates two separate groups.
- Non-adjacent members. Group members do not need to sit next to each other. Other elements can sit between them and the grouping still works.
- Older browsers. Browsers that do not support
nameondetailsignore the attribute. The items then behave as independent widgets. The content stays reachable, so this is a graceful fallback. Check current browser support tables before depending on exclusive behavior. - Names are page-wide. Reusing
name="faq"in two unrelated sections of the same page merges them into one group. Use distinct names such asfaq-billingandfaq-account.
Exercises
Exercise
You have six details elements on a page. Three are billing questions and three are shipping questions. You want one open item at a time within each topic, but a billing item and a shipping item may be open together. How do you set the name attributes?
Show answer
Give the three billing elements one shared name, such as name="billing", and the three shipping elements a different shared name, such as name="shipping". Each name defines its own group, so each group allows one open item.
Exercise
A visitor has item A open and clicks item B in the same group. How many toggle events fire, and how can your handler react only to the item that opened?
Show answer
Two events fire: one on A as it closes and one on B as it opens. Inside the handler, check item.open (or event.newState === 'open') and act only when it is true.