Event Bubbling and Capturing in JavaScript
Understand event bubbling and capturing in JavaScript: the three phases, the capture flag and stopPropagation, with a clear explanation and output.
This code needs a web page. Add the HTML elements it looks up by id, link the script, and open the page in a browser. Node has no document object. Use <div id="parent"><button id="child">Click</button></div>.
1// Event Bubbling (default)2// Event flows from target to root34const parent = document.getElementById('parent');5const child = document.getElementById('child');67// Bubbling phase (default)8parent.addEventListener('click', function(e) {9 console.log('Parent clicked (bubbling)');10});1112child.addEventListener('click', function(e) {13 console.log('Child clicked (bubbling)');14 // e.stopPropagation(); // Stop bubbling15});1617// Clicking child will log:18// 1. Child clicked (bubbling)19// 2. Parent clicked (bubbling)2021// Event Capturing (useCapture = true)22// Event flows from root to target2324parent.addEventListener('click', function(e) {25 console.log('Parent clicked (capturing)');26}, true); // true = capturing phase2728child.addEventListener('click', function(e) {29 console.log('Child clicked (capturing)');30}, true);3132// Clicking child will log:33// 1. Parent clicked (capturing)34// 2. Child clicked (capturing)3536// Method 3: Both phases37parent.addEventListener('click', function(e) {38 console.log('Parent - Capturing');39}, true);4041parent.addEventListener('click', function(e) {42 console.log('Parent - Bubbling');43}, false);4445child.addEventListener('click', function(e) {46 console.log('Child - Capturing');47}, true);4849child.addEventListener('click', function(e) {50 console.log('Child - Bubbling');51}, false);5253// Clicking child logs:54// 1. Parent - Capturing55// 2. Child - Capturing56// 3. Child - Bubbling57// 4. Parent - Bubbling5859// Method 4: Stop propagation60child.addEventListener('click', function(e) {61 e.stopPropagation(); // Stop at this element62 console.log('Event stopped');63});6465// Method 5: Stop immediate propagation66child.addEventListener('click', function(e) {67 e.stopImmediatePropagation(); // Stop all handlers68 console.log('All handlers stopped');69});
Sample Output
Parent clicked (capturing)
Capture handlers on the parent run first, on the way down.
Event stopped
Method 4 calls stopPropagation, so no parent bubbling handler prints.
In this one
- The capturing, target and bubbling phases
- How the third argument of addEventListener selects capturing
- The order handlers run in
- The difference between stopPropagation and stopImmediatePropagation
How it works
- 1
parent and child are found by id.
- 2
Bubbling listeners are added to both.
- 3
Capturing listeners are added with true.
- 4
Method 3 adds both phases to show the full order.
- 5
Method 4 calls stopPropagation on the child.
- 6
Method 5 calls stopImmediatePropagation.
Edge cases
All methods active at once
Because Method 4 stops bubbling, parent bubbling handlers never run in this full file.
Comment out Methods 4 and 5 to see the full bubbling order.
Events that do not bubble
focus and mouseenter do not bubble.
Use focusin or mouseover if you need bubbling versions.
Overusing stopPropagation
Analytics or dropdown-close code on the document stops working.
Only stop propagation when you really need to.
What's going on
When you click an element that sits inside another element, both can hear the click. Event propagation is the path the event travels through the page. It has three phases.
First is capturing. The event goes down from the root of the page to the clicked element. Next is the target phase, when it reaches the element itself. Last is bubbling. The event goes back up from the target to the root. Think of a stone dropped into a well. It falls down, hits the bottom, and the ripple comes back up.
Bubbling is the default and the most common case. A normal addEventListener runs in the bubbling phase. So clicking the child logs "Child clicked" first and "Parent clicked" second. It is bottom-up.
To listen during capturing, pass true as the third argument. Capturing is top-down, so the parent runs before the child.
Method 3 adds both kinds on both elements. Clicking the child logs Parent capturing, Child capturing, Child bubbling, Parent bubbling. That order shows the whole round trip.
stopPropagation() stops the event from moving to other elements. Parent handlers will not run. stopImmediatePropagation() is stronger. It also stops any remaining handlers on the same element.
Propagation is what makes event delegation possible, where one parent listener handles clicks on many children. It also helps performance and complex interactions. In this file, Method 4 stops bubbling at the child, so the parent bubbling handlers do not run when Method 4 is active.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
With default listeners, clicking a child runs which first?
Pick an answer to check it.
Question 2
What does passing true as the third argument do?
Pick an answer to check it.
Questions people ask
Which phase runs by default?
Bubbling. Pass true or { capture: true } for capturing.
Does the target run capture or bubble handlers first?
Modern browsers run capture handlers on the target before bubble handlers.
Why does "All handlers stopped" still print?
stopImmediatePropagation only blocks handlers that come after it. The handler that calls it finishes its own lines.