You click a button inside a card inside a page. Did you click the button, the card or the page?
Keep the question in mind. By the end of the lesson you will be able to answer it, and prove it with working code.
02Start with working code
One click on the button runs the button's listener and then the div's listener: the event bubbles up through its ancestors.
for (const id of ["outer", "inner"]) {
document.getElementById(id).addEventListener("click", () => console.log("click on", id));
}
document.getElementById("inner").click();
03Objective and prerequisites
Learning objective: Trace how an event travels through capture, target and bubble phases, and control it with stopPropagation and the capture option.
When you click an element, the event does not stay there. It bubbles up to the parent, then the grandparent, all the way to document and window.
So a listener on a container also hears clicks on everything inside it. That is useful, but it can also trigger handlers you did not expect.
event.stopPropagation() stops the event from travelling any further up. Use it sparingly, because other code may rely on hearing the event.
Before bubbling there is a capture phase that travels down from the window to the target. Listeners added with { capture: true } run during that trip down.
The full order is: capture listeners from window down to the target's parent, then listeners on the target itself, then bubble listeners from the parent back up. event.eventPhase reports 1 (capture), 2 (at target) or 3 (bubble).
stopPropagation stops travel to other elements but lets remaining listeners on the current element run; stopImmediatePropagation stops those too.
Not every event bubbles: focus, blur, mouseenter and mouseleave do not; use focusin/focusout or mouseover/mouseout when you need bubbling versions. event.bubbles tells you.
Events created with new Event(type) do not bubble unless you pass { bubbles: true }. Forgetting this is a common reason a test's dispatched event never reaches a delegated listener.
05Definitions
Event propagation
The path an event travels through the DOM: down in the capture phase and up in the bubble phase.
Bubbling
The phase in which an event travels from the target up through its ancestors.
Capturing
The phase in which an event travels from the window down to the target, before bubbling.
stopPropagation
A method that stops an event from reaching any further elements.
eventPhase
A number showing the current phase: 1 capture, 2 at target, 3 bubble.
ex-058-01Bubbling order Browser page (sandboxed preview)
A click on the innermost element is heard by every ancestor, inside out.
for (const id of ["page", "card", "like"]) {
document.getElementById(id).addEventListener("click", () => console.log(id));
}
document.getElementById("like").click();
Sample input
None
Expected output
like
card
page
Common mistake
Expecting the order to follow when the listeners were added; bubbling order follows the tree.
const path = [];
for (const id of ["a", "b", "c"]) {
const el = document.getElementById(id);
// TODO: add a capture listener that pushes id + ":down"
// TODO: add a bubble listener that pushes id + ":up"
}
document.getElementById("c").click();
console.log(path.join(" "));
Close when clicking outside
Use target checks instead of stopPropagation.
// Write onOverlayClick(event): set closed = true only when the click
// landed on the overlay itself, not on the panel or anything inside it.
let closed = false;
09Prediction
Commit to an answer first, then run the real code to compare.
Most design systems close an open dropdown when you click anywhere else. They listen for clicks on the document and check whether event.target is inside the dropdown with contains(). That works because every click bubbles up to the document.
6. To close a modal when its backdrop is clicked, which is more robust?
stopPropagation in every element inside the modal
Check event.target === event.currentTarget in the backdrop listener
Add listeners to every child
Use a timer
13Solutions and tests
Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.
Automated acceptance tests for this lesson
These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.
The row also opens: Prints archive; Clicking the row still opens
Trace the path: Prints the full path; Six steps recorded
Close when clicking outside: Prints false then true; No stopPropagation
The lesson lives in the companion repository at curriculum/level-03-interactive-web-applications/module-12-events-and-interaction/lesson-058-event-bubbling-and-capturing/. Every example on this page is generated from those files.
These links open the files in the GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Engineering-Academy). You can also use the ZIP download above.
15Summary and next steps
Events travel down (capture), reach the target, then travel up (bubble).
Ancestors hear events from their descendants.
{ capture: true } runs a listener on the way down.
stopPropagation stops the journey; prefer target checks where you can.
Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.
Concept explanationevent bubbling and capturing
Context: I am learning event bubbling and capturing in JavaScript.
Task: Explain capture, target and bubble phases with nested elements and logged listeners.
Constraints: Beginner-friendly language, short runnable examples, no libraries.
Expected behaviour: I can explain the idea in my own words and predict small programs.
Testing: Finish with three quick questions for me to answer.
Code predictionevent bubbling and capturing drill
Context: I have practised event bubbling and capturing.
Task: Give me six nested-element programs and ask which listeners run and in what order.
Constraints: Wait for my answer before revealing each result.
Expected behaviour: Each question has exactly one correct output.
Testing: Explain any I miss with a step-by-step trace.
Debuggingevent bubbling and capturing bug
Context: A click on a button inside a table row also opens the row.
Task: Help me find the cause with questions instead of giving me the fix.
Constraints: Do not rewrite my program.
Expected behaviour: I locate and fix the bug myself.
Testing: Suggest the console.log lines that would reveal the problem.
Exercise generationevent bubbling and capturing practice
Context: I finished the event bubbling and capturing exercises.
Task: Create three propagation exercises with exact expected orders.
Constraints: Fictional data and exact expected output.
Expected behaviour: Each exercise has tests I can run.
Testing: Put the solutions in a separate hidden section.
Code reviewevent bubbling and capturing review
Context: Here is my code that uses event bubbling and capturing.
Task: Review my code for stopPropagation calls that might break other features.
Constraints: Explain each suggestion briefly and keep behaviour identical.
Expected behaviour: The code is clearer and has no hidden mutation or edge-case bug.
Testing: List the changes in order of importance.