Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 5 of 10 · DOM Manipulation and Events
Interview question

What is the difference between event bubbling and event capturing? Event bubbling और event capturing में क्या अंतर है?

Answer

These are the two phases of DOM event propagation, describing the order in which nested elements receive an event.

PhaseDirectionDefault behavior
CapturingTop (document) down to the target elementOff by default; opt-in with { capture: true }
BubblingTarget element up to the top (document)Default phase for most listeners
// HTML structure: <div id='outer'><div id='inner'><button id='btn'></button></div></div>

const outer = document.getElementById('outer');
const inner = document.getElementById('inner');
const btn = document.getElementById('btn');

// Bubbling phase listeners (default)
outer.addEventListener('click', () => console.log('Outer (bubble)'));
inner.addEventListener('click', () => console.log('Inner (bubble)'));
btn.addEventListener('click', () => console.log('Button (bubble)'));

// Clicking the button logs (bubbling goes target -> up):
// Button (bubble)
// Inner (bubble)
// Outer (bubble)

// Capturing phase listeners
outer.addEventListener('click', () => console.log('Outer (capture)'), { capture: true });
inner.addEventListener('click', () => console.log('Inner (capture)'), { capture: true });

// Full order when both phases have listeners (capture goes down first, then bubble goes up):
// Outer (capture)
// Inner (capture)
// Button (bubble)
// Inner (bubble)
// Outer (bubble)

// Stopping propagation
btn.addEventListener('click', (event) => {
    event.stopPropagation(); // prevents the event from bubbling further up
    console.log('Button clicked, propagation stopped');
});
// Now 'Inner (bubble)' and 'Outer (bubble)' won't fire

// stopImmediatePropagation also stops OTHER listeners on the SAME element
btn.addEventListener('click', (e) => {
    e.stopImmediatePropagation();
});
btn.addEventListener('click', () => {
    console.log('This never runs due to stopImmediatePropagation');
});

ये DOM event propagation के दो phases हैं, जो बताते हैं कि nested elements किस क्रम में event receive करते हैं।

PhaseDirectionDefault
CapturingTop (document) से target तकDefault off, capture: true से enable
BubblingTarget से top तकज़्यादातर listeners के लिए default
const outer = document.getElementById('outer');
const inner = document.getElementById('inner');
const btn = document.getElementById('btn');

// Bubbling phase (default)
outer.addEventListener('click', () => console.log('Outer (bubble)'));
inner.addEventListener('click', () => console.log('Inner (bubble)'));
btn.addEventListener('click', () => console.log('Button (bubble)'));

// Button click पर order (target से ऊपर):
// Button (bubble)
// Inner (bubble)
// Outer (bubble)

// Capturing phase
outer.addEventListener('click', () => console.log('Outer (capture)'), { capture: true });

// stopPropagation
btn.addEventListener('click', (event) => {
    event.stopPropagation(); // ऊपर जाना रोकता है
});

// stopImmediatePropagation - same element के अन्य listeners भी रोकता है
btn.addEventListener('click', (e) => {
    e.stopImmediatePropagation();
});

Was this answer clear?