{"success":true,"name":"attach-one-listener-to-the-parent-when-child-elements-come-a","description":"Click handlers on list items that are re-rendered, added, or removed","category":"ai-brimind","path":"ai-brimind/attach-one-listener-to-the-parent-when-child-elements-come-a/SKILL.md","file":"SKILL.md","content":"---\nname: attach-one-listener-to-the-parent-when-child-elements-come-a\ndescription: \"Click handlers on list items that are re-rendered, added, or removed\"\n---\n\n# Attach One Listener to the Parent When Child Elements Come and Go\n\n## When to use\nYou are wiring events onto elements that get created or replaced dynamically, or clicks on new items mysteriously do nothing.\n\n## Example request\nlisteners attached on page load → rows added later have none; dead new elements. list re-rendered after a fetch → old nodes replaced, handlers gone with them; wiring lost on refresh. a listener per row on a 500-row table → 500 registrations to manage; per-item bloat. — each tied handlers to nodes that don't stay.\n\n## Instructions\nAct as a receptionist who sits at the building entrance and greets everyone, instead of hiring one greeter per office that keeps getting remodeled. When child elements are dynamic — rendered from data, added, removed, replaced — do not attach handlers to the children: attach one listener to a stable parent that never gets replaced, and on each event, identify which child was the actual target (walk up from the event's origin to the matching item, using a selector or data attribute) and act on it. This survives every re-render for free: new items are handled the moment they exist, with zero registration, and there is nothing to clean up per item. Store the item's identity on the element (a data attribute with its ID), so the handler reads what was clicked instead of relying on position. Verify the parent chosen truly persists across renders — delegating to a node that also gets replaced re-creates the bug one level up. When 'clicks stopped working after refresh/update' appears, suspect replaced nodes before broken logic. Only proceed once dynamic regions have exactly one durable listener, on an element that outlives all its children.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #747\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}