🧩 HTML5: Fix a Details Toggle Not Reaching an Assistive Reader
The native <details>/<summary> pair is accessible by default — screen readers announce it as an expandable disclosure widget automatically. That default behavior silently breaks the moment a click handler calls event.preventDefault() on the summary’s click event to drive a custom animation, because preventing the default click also prevents the browser’s own toggle of the open attribute, leaving the element’s accessible state permanently out of sync with what’s visually showing on screen.
🔎 The Problem
summaryEl.addEventListener('click', (event) => {
event.preventDefault(); // blocks the native open/close toggle
animateExpand(detailsEl); // custom JS animation instead
});
// Visually expands fine with the animation
// Screen reader: still announces "collapsed", open attribute
// on <details> never actually changes
✅ Fix: Toggle the Real Attribute, Don’t Just Animate Around It
- Instead of calling
preventDefault(), let the native toggle happen and animate based on the resultingopenattribute change (listen for the element’s owntoggleevent), so the accessible state and the visual state always update together. - If a custom open/close animation genuinely requires intercepting the click, set
detailsEl.openprogrammatically right after callingpreventDefault(), so the attribute still flips even though the browser’s default handling was blocked. - Test with a real screen reader (NVDA or VoiceOver, not just DevTools) after any change here — the accessibility tree can report a state that looks fine in the Elements panel but is still announced incorrectly to assistive tech.
🚨 Why This Is Dangerous
- A screen reader user can be told content is still collapsed when it’s actually open and fully visible on screen, leading them to either miss the content entirely or try to re-activate a toggle that’s already in the state they wanted.
An element that looks expanded on screen but still reports itself as collapsed isn’t accessible — it’s just lying to the one audience that can’t double-check it visually.




