01 / 22

Event
Propagation

Learn how browser events travel through the DOM tree: from the window down to the target and back up again via capturing and bubbling.

JavaScript

What Is a DOM Event?

Every time a user clicks, scrolls, types, or hovers, the browser creates an event object and delivers it to the element involved. That delivery journey is called event propagation.

click
User presses and releases a mouse button on an element
keydown
User presses any key on the keyboard
submit
User submits a form element

The DOM Tree Structure

HTML elements form a nested tree. Every element has a parent, and the root is the document (or window). Events propagate along this hierarchy.

window <html> <body> <head> <div> <button> TARGET

The Three Phases of Propagation

Every DOM event goes through three distinct phases, always in the same order. Most developers only interact with phase 2 and 3, but understanding all three gives you full control.

1. Capturing
Event travels down from the window to the target's parent
2. Target
Event arrives at the actual element that was interacted with
3. Bubbling
Event travels up from the target back to the window

Phase 1: Capturing

window document <body> <div> <button> CAPTURE

During capturing, the event starts at the window and drills downward through each ancestor until it reaches the target's parent. Handlers registered with capture: true fire during this phase.

// Listen during capture phase document.addEventListener('click', handler, true);

Phase 3: Bubbling

window document <body> <div> <button> BUBBLE

After reaching the target, the event bubbles up through every ancestor back to the window. This is the default behavior: most event handlers fire during the bubbling phase.

// Default: listens during bubble phase document.addEventListener('click', handler); // Equivalent to: document.addEventListener('click', handler, false);

The Complete Propagation Path

window document <body> <div> <button> CAPTURE BUBBLE TARGET PHASE

Capturing vs Bubbling

Capturing Bubbling
Direction Window → Target Target → Window
Phase Number 1 3
Default? No (opt-in) Yes (default)
addEventListener 3rd arg = true 3rd arg = false or omitted
Common Use Intercepting before target Event delegation

event.target vs event.currentTarget

event.target
The original element that triggered the event (where the click actually happened)
event.currentTarget
The element whose listener is currently running (where you attached addEventListener)
const div = document.querySelector('div'); div.addEventListener('click', function(e) { // If user clicks the <button> inside this div: e.target; // <button> (what was clicked) e.currentTarget; // <div> (where listener lives) });

Stopping the Propagation

You can stop an event from continuing its propagation path using two methods. This prevents ancestor (or descendant) handlers from firing.

stopPropagation()
Stops the event from traveling to the next element in the chain. Other handlers on the same element still fire.
stopImmediatePropagation()
Stops propagation and prevents any remaining handlers on the same element from firing.
button.addEventListener('click', function(e) { e.stopPropagation(); // The click won't bubble up to <div>, <body>, etc. });

Event Delegation Pattern

Because events bubble, you can attach one listener on a parent instead of individual listeners on every child. This is called event delegation: a powerful, efficient pattern.

<ul> ONE listener here <li> Item 1 <li> Item 2 <li> Item 3 Click bubbles up to <ul> Use e.target to identify which <li>
const list = document.querySelector('ul'); list.addEventListener('click', function(e) { if (e.target.tagName === 'LI') { console.log('Clicked:', e.target.textContent); } });

How to Apply This Knowledge

1
Identify the target element: Which element does the user interact with?
2
Trace the ancestor chain: List every parent from window down to the target.
3
Choose your phase: Do you need to intercept before the target (capture) or after (bubble)?
4
Attach your listener: Use addEventListener with the appropriate third argument.
5
Consider delegation: Can you attach one handler on a parent instead of many on children?

Example 1: Basic Bubbling

What order do the alerts appear when the button is clicked?

<div id="outer"> <button id="inner">Click Me</button> </div> document.getElementById('inner') .addEventListener('click', () => alert('Button!')); document.getElementById('outer') .addEventListener('click', () => alert('Div!'));

Example 2: Capture + Bubble

Now the div listens during capturing. What changes?

document.getElementById('inner') .addEventListener('click', () => alert('Button!')); document.getElementById('outer') .addEventListener('click', () => alert('Div!'), true);

Example 3: stopPropagation in Action

What happens when the button's handler calls stopPropagation?

document.getElementById('inner') .addEventListener('click', (e) => { e.stopPropagation(); alert('Button!'); }); document.getElementById('outer') .addEventListener('click', () => alert('Div!'));

Common Mistakes

✕ Overusing stopPropagation
Stopping propagation can silently break other listeners (analytics trackers, modal closers) that rely on events bubbling to ancestors.
✕ Confusing target and currentTarget
Using event.target when you meant event.currentTarget causes handlers to reference the wrong element, especially in delegation.
✕ Forgetting Non-Bubbling Events
Some events like focus, blur, and scroll do not bubble. Use focusin/focusout for delegation instead.

Summary Cheat Sheet

Concept Key Detail
Capturing Phase 1: window → target (3rd arg = true)
Target Phase 2: event arrives at the clicked element
Bubbling Phase 3: target → window (default behavior)
event.target The element that originated the event
event.currentTarget The element the listener is attached to
stopPropagation() Prevents event from reaching other elements
Event Delegation One listener on parent, check event.target

Question 1 of 5

In which order do the three phases of event propagation occur?

Question 2 of 5

What is the default phase that addEventListener listens on when no third argument is provided?

Question 3 of 5

Which handler(s) will fire?

Question 4 of 5

In the event delegation pattern, where do you attach the event listener?

Question 5 of 5

Which handler fires first?