{"success":true,"name":"innerhtml-rebuilds-the-whole-container-listeners-die-inputs-","description":"Appending content with innerHTML += and existing elements stop working","category":"ai-brimind","path":"ai-brimind/innerhtml-rebuilds-the-whole-container-listeners-die-inputs-/SKILL.md","file":"SKILL.md","content":"---\nname: innerhtml-rebuilds-the-whole-container-listeners-die-inputs-\ndescription: \"Appending content with innerHTML += and existing elements stop working\"\n---\n\n# innerHTML += Rebuilds the Whole Container: Listeners Die, Inputs Reset, Focus Is Lost\n\n## When to use\nVanilla JS code grows or updates a container via innerHTML concatenation, and previously attached handlers, typed input, or focus mysteriously vanish afterward.\n\n## Example request\nlist grown with container.innerHTML += newRow → every existing row was destroyed and recreated as a lookalike; all their listeners died with the originals. user typing in a field inside the container → one += elsewhere wipes their text; collateral reset. focused element replaced by its clone → focus falls to nowhere — on TV, per the focus rules, the user is stranded; killed cursor. — each 'append' was secretly a demolition and rebuild.\n\n## Instructions\nAct as a renovator who knows this contractor's 'add a room' works by bulldozing the house and rebuilding it with the room attached — the furniture, the tenants, and everything not in the blueprint is lost. innerHTML += is not an append: it serializes the container's current DOM to a string, concatenates, and re-parses the whole thing — every existing element is destroyed and replaced by a fresh lookalike, so their event listeners (attached to the dead originals), input values the user typed, scroll positions, and focus all evaporate; the operation also gets slower as the container grows, re-parsing everything to add anything. True appends exist — use them: build the new content as elements (or via insertAdjacentHTML, which parses ONLY the new fragment and touches nothing existing) and add it with append — existing nodes remain the same living objects throughout. For updates rather than additions, prefer targeted changes to the specific element — the partial-row-update pattern this project already follows — over regenerating containers. Two companion rules: event delegation on the stable parent, per the delegation rules, makes handlers survive any child churn — the right architecture for dynamic lists regardless; and never build HTML strings from user data anyway, per the XSS rules — element creation with text assignment is both safer and immune to this trap. When 'clicks stopped working after adding an item' appears, this is the first suspect. Only proceed once nothing that must survive — handlers, typed input, focus — lives inside a container that gets string-rebuilt.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #831\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}