Events & event delegation
Understand the idea
Newly added buttons look like the old ones but ignore clicks. The code attached listeners only to the buttons that existed at startup.
Follow the event’s path
A button added after page load still needs to respond. Attaching a listener to every existing button misses future buttons and can repeat work. Delegation keeps one listener on a parent that stays in the document, then checks which descendant the interaction came from.
Listen at a stable parent
A listener connects an event type, such as click, to a . Pass the callback instead of calling it during registration. Many events travel through capture toward their target and then bubble outward; not every event bubbles, so confirm the event's behavior before depending on delegation.
For many events, the browser first travels down toward the target during capture, then back up through ancestors during bubbling. A normal click listener uses the bubbling phase. Not every event bubbles; focusin can be a better delegation choice than focus. Use the event's actual behavior rather than assuming all events have the same path.
| Choice | Meaning | When to use it |
|---|---|---|
| event.target | Where the event started | Work out which descendant was activated |
| event.currentTarget | Where this listener is registered | Refer to the stable delegation parent |
| closest(selector) | Find the nearest matching element, including itself | Handle clicks on a nested icon or label |
| preventDefault() | Cancel a cancelable browser default action | Handle a form without its normal navigation |
| stopPropagation() | Stop later propagation along the event path | Only when that interruption is intentional |
| AbortSignal / removeEventListener | Stop a listener when it is no longer needed | Avoid duplicate behavior after remounting |
Find the intended control
event.target is the node where the event originated. event.currentTarget is the node whose listener is currently running. A nested icon can be the target even when the user meant to activate a button. closest finds a relevant ancestor, and a containment check keeps the match within your delegated region.
event.target is where the event originated. event.currentTarget is the element whose listener is currently running. Clicking an icon inside a button makes the icon the target, so checking only whether target is a button can miss the click. closest('button') walks up to the intended control, and contains checks that the match still belongs to this parent.
Change the page safely
A parent listener can handle descendants added later because the event path still passes through the parent. This reduces repeated registration and makes dynamic lists easier to maintain. It does not mean every click is relevant: filter the message before doing work.
Keep keyboard interactions included
preventDefault cancels a cancelable browser action, such as form submission. stopPropagation changes the event's path; it does not cancel that default action. Prefer native buttons for actions so keyboard activation follows the same click handler. Remove listeners when their owning feature is disposed.
A real button already supports keyboard activation, so its click event works for more than mouse users. A clickable div does not automatically provide that behavior. The example programmatically clicks once so you can inspect the result immediately; you can also press the rendered button yourself.
Mental model
FOLLOW THE VALUE
The event begins at the clicked descendant.
01 / 03Trace the transformation
Live example
The parent owns one listener. The final line activates the button for a reproducible console result; you can also click it or focus it and press Enter. The inserted markup is a fixed trusted string.
Your console output appears here.
Guided challenge
Listen for the actual interaction
Register the delegated handler for a button activation so the final click logs Add.
Your console output appears here.
EXPECTED CONSOLE OUTPUT
Add
Hints 0/2
Use all hints or make 3 failed runs (0/3).
Free sandbox
Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.
Your console output appears here.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 150 XP. You can always revisit it.