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.
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.
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.
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.
Phase 1: Capturing
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.
Phase 3: Bubbling
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.
The Complete Propagation Path
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
Stopping the Propagation
You can stop an event from continuing its propagation path using two methods. This prevents ancestor (or descendant) handlers from firing.
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.
How to Apply This Knowledge
Example 1: Basic Bubbling
What order do the alerts appear when the button is clicked?
Example 2: Capture + Bubble
Now the div listens during capturing. What changes?
Example 3: stopPropagation in Action
What happens when the button's handler calls stopPropagation?
Common Mistakes
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
A <button> is nested inside a <div>. Both have click listeners. The button's handler calls event.stopPropagation(). The user clicks the button.
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
A <div> has a click handler registered with the third argument set to true. Inside the div, there is a <span> with its own click handler (default). The user clicks the span.
Which handler fires first?