details And summary Build Native Disclosures
details and summary give a native disclosure widget whose open property is both state and API, and the toggle event reports each open/close transition.
Code
// <details>/<summary> is a native disclosure widget: it opens and closes
// with no JavaScript at all, and open is both state and API
const details = document.createElement('details');
const summary = document.createElement('summary');
summary.textContent = 'Deployment steps';
details.append(summary, '1. build 2. verify 3. promote');
document.body.append(details);
console.log('starts closed:', details.open);
// Setting the property expands the widget immediately
details.open = true;
console.log('opened via property:', details.open);
// toggle reports each open/close transition; register the listener
// before flipping state so the change below is observed
let events = 0;
details.addEventListener('toggle', () => {
events++;
console.log('toggle -> open:', details.open);
});
details.open = false;
setTimeout(() => {
console.log('toggle events observed:', events);
console.log('content still present:', details.textContent.includes('promote'));
details.remove();
}, 0);What the code does
The demo data lives in module-level bindings: details, summary, events.
Points of care
- It is dependency-free in the strict sense — the import list, if any, is empty of packages.
- A browser is the runtime here:
documentare not available in Node.
Using it
The demo output is labeled (starts closed:, opened via property:, toggle -> open:), which makes each printed line traceable to the operation that emitted it.
Key statements
details.addEventListener('toggle', () => {
events++;
console.log('toggle -> open:', details.open);
});
setTimeout(() => {
console.log('toggle events observed:', events);
console.log('content still present:', details.textContent.includes('promote'));
details.remove();
}, 0);
Interactive Tools for This Topic
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.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.
FAQ
- Does details And summary Build Native Disclosures 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.