Event Bubbling and Capturing

Understand event propagation phases

IntermediateTopic: Events Programs
Back

JavaScript Event Bubbling and Capturing Program

This program helps you to learn the fundamental structure and syntax of JavaScript programming.

Try This Code
// Event Bubbling (default)
// Event flows from target to root

const parent = document.getElementById('parent');
const child = document.getElementById('child');

// Bubbling phase (default)
parent.addEventListener('click', function(e) {
    console.log('Parent clicked (bubbling)');
});

child.addEventListener('click', function(e) {
    console.log('Child clicked (bubbling)');
    // e.stopPropagation(); // Stop bubbling
});

// Clicking child will log:
// 1. Child clicked (bubbling)
// 2. Parent clicked (bubbling)

// Event Capturing (useCapture = true)
// Event flows from root to target

parent.addEventListener('click', function(e) {
    console.log('Parent clicked (capturing)');
}, true); // true = capturing phase

child.addEventListener('click', function(e) {
    console.log('Child clicked (capturing)');
}, true);

// Clicking child will log:
// 1. Parent clicked (capturing)
// 2. Child clicked (capturing)

// Method 3: Both phases
parent.addEventListener('click', function(e) {
    console.log('Parent - Capturing');
}, true);

parent.addEventListener('click', function(e) {
    console.log('Parent - Bubbling');
}, false);

child.addEventListener('click', function(e) {
    console.log('Child - Capturing');
}, true);

child.addEventListener('click', function(e) {
    console.log('Child - Bubbling');
}, false);

// Clicking child logs:
// 1. Parent - Capturing
// 2. Child - Capturing
// 3. Child - Bubbling
// 4. Parent - Bubbling

// Method 4: Stop propagation
child.addEventListener('click', function(e) {
    e.stopPropagation(); // Stop at this element
    console.log('Event stopped');
});

// Method 5: Stop immediate propagation
child.addEventListener('click', function(e) {
    e.stopImmediatePropagation(); // Stop all handlers
    console.log('All handlers stopped');
});
Output
// Output depends on which element is clicked

Understanding Event Bubbling and Capturing

Event propagation has two phases.

Bubbling Phase (default)

Event flows from target to root
Bottom-up propagation
Most common use case

Capturing Phase

Event flows from root to target
Top-down propagation
Use third parameter: true

Event Flow

1.Capturing: Root → Target
2.Target: At target element
3.Bubbling: Target → Root

stopPropagation()

Stops event from propagating
Prevents parent handlers

stopImmediatePropagation()

Stops all handlers on element
More aggressive than stopPropagation()

Use Cases

Event delegation
Performance optimization
Complex interactions

Let us now understand every line and the components of the above program.

Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.

Practical Learning Notes for Event Bubbling and Capturing

This JavaScript program is part of the "Events Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents