details name Attribute Exclusively Opens Panels
Give grouped details panels a shared name attribute: opening one closes the others automatically, an exclusive accordion with zero JavaScript.
Code
// <details name> groups panels into an exclusive accordion: opening one
// panel closes the others in the same group, with zero JavaScript
for (const title of ['Plan', 'Build', 'Ship']) {
const panel = document.createElement('details');
panel.name = 'release-phase';
const summary = document.createElement('summary');
summary.textContent = title;
panel.append(summary, title + ' phase steps');
document.body.append(panel);
}
const panels = document.querySelectorAll('details[name="release-phase"]');
console.log('grouped panels:', panels.length);
const first = panels[0];
first.open = true;
console.log('first panel open:', first.open);
// setting open on the second panel closes the first one automatically:
// the spec runs exclusivity on the open attribute change itself
panels[1].open = true;
setTimeout(() => {
for (const panel of panels) {
console.log(panel.querySelector('summary').textContent, 'open:', panel.open);
}
}, 0);How it works
The core work runs in nested loops — outer for (const title of ['Plan', 'Build', 'Ship']), inner for (const panel of panels).
The file’s state is a handful of top-level bindings: panels, first.
What to watch for
documentexist only in browsers — a Node runtime has no shims for them out of the box.
Putting it to use
Output lines are prefixed (grouped panels:, first panel open:), making each step’s effect easy to spot.
Core lines
setTimeout(() => {
for (const panel of panels) {
console.log(panel.querySelector('summary').textContent, 'open:', panel.open);
}
}, 0);
Interactive Tools for This Topic
CSS Color Name LookupTranslate between CSS keyword color names and hex values with RGB and HSL conversions, exact name matching, and nearest-color suggestions.DOM Event Propagation VisualizerVisualize DOM event capturing, targeting, and bubbling phases in real time. Toggle stopPropagation and stopImmediatePropagation to see how events traverse the tree.HTML Entity Encoder / DecoderEncode the five HTML-significant characters and decode named or numeric entities, with optional numeric encoding for non-ASCII text.Markdown Live PreviewRender Markdown to HTML in real time with a live preview pane. Supports headings, lists, code blocks, links, emphasis, and blockquotes for documentation drafting.
FAQ
- Does details name Attribute Exclusively Opens Panels run in plain Node.js?
- No — it touches `document`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.
Report a problem with this exampleOpens a pre-filled issue on GitHub.