Skip to content
engineering workbench.by StudyPort Academy
Learning path
Intermediate20 minLESSON 17 / 34

Events & event delegation

01

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.

The tools of a delegated event handler
ChoiceMeaningWhen to use it
event.targetWhere the event startedWork out which descendant was activated
event.currentTargetWhere this listener is registeredRefer to the stable delegation parent
closest(selector)Find the nearest matching element, including itselfHandle clicks on a nested icon or label
preventDefault()Cancel a cancelable browser default actionHandle a form without its normal navigation
stopPropagation()Stop later propagation along the event pathOnly when that interruption is intentional
AbortSignal / removeEventListenerStop a listener when it is no longer neededAvoid 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.

02

Mental model

FOLLOW THE VALUE

button activation
event target
button

The event begins at the clicked descendant.

01 / 03Trace the transformation

03

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.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022
04

Guided challenge

Listen for the actual interaction

Register the delegated handler for a button activation so the final click logs Add.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

Add

Hints 0/2

    Use all hints or make 3 failed runs (0/3).

    05

    Free sandbox

    Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01Which property refers to the node whose listener is running?
    02Why can delegation handle a later-added button?

    Ready to call this one understood?

    Mark this lesson complete to earn 150 XP. You can always revisit it.

    Sign in to save progress
    Next lesson