🧩 Your FAQ Section Keeps Snapping Shut on Its Own
The native `<details>`/`<summary>` element is a convenient, no-JavaScript way to build collapsible sections – but if any code re-renders that part of the page (a framework re-render, an innerHTML replacement, or content streamed in after the initial load), the `open` attribute is reset to whatever the fresh markup says, silently collapsing a section the user had deliberately opened.
🔎 The Problem
<details id="faq-1">
<summary>How do refunds work?</summary>
<p>Refunds are processed within 5-7 business days...</p>
</details>
<script>
// A later re-render replaces this section'"'"'s markup - e.g. after new
// data arrives, or a framework diffs and re-renders this subtree:
document.getElementById("faq-container").innerHTML = renderFaqList(faqs);
// The new markup doesn'"'"'t include `open` on any <details> element, so
// every section the user had expanded snaps back closed the instant
// the re-render happens - with no error, and often no obvious link
// back to the re-render being the cause.
✅ Fix: Preserve Open State Across the Re-Render
- Before replacing the markup, read which `<details>` elements currently have the `open` attribute set (`document.querySelectorAll(‘details[open]’)`) and store their identifiers, then re-apply `open` to the matching elements in the new markup right after it’s inserted.
- In a component framework (React, Vue, etc.), track expanded/collapsed state explicitly in application state rather than relying on the DOM’s own `open` attribute to survive a re-render – the framework then controls `open` deliberately on every render instead of leaving it to chance.
- If the re-render is triggered by something unrelated to the FAQ content itself (a sibling section updating, a global state change), consider scoping the re-render more narrowly so the `<details>` elements aren’t touched at all when nothing about them actually changed.
⚠️ Why It Feels Like a Browser Bug
- The `<details>` element’s own toggle behavior works perfectly on its own – this isn’t a browser inconsistency, it’s that the re-render is generating fresh markup without the `open` attribute, which looks indistinguishable from a browser bug unless you specifically check what the re-render produced.
- This exact issue shows up anywhere content is periodically refreshed from a data source – a live-updating FAQ, a notifications panel, a collapsible log viewer – any of which can silently reset user-expanded sections on every refresh cycle.
The browser remembers exactly what you told it to render – if the new markup doesn’t say a section is open, there’s nothing for the browser to remember on your behalf.




