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.

JavascriptData Integrity

Code

javascriptsnippet
26 lines
// <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

  • document exist 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

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.