← Back to JavaScript Course | Chapter 10: DOM Advanced | Lesson 2 of 8

JS Event Bubbling और Capturing

जब आप कुछ click करते हैं, तो click उसके parents के ज़रिए ऊपर जाती है (bubbling) या ऊपर से नीचे आती है (capturing), जैसे किसी तालाब में लहरें। आप उस लहर को फैलने से रोक सकते हैं।
Syntax
javascript
element.addEventListener("click", handler, false);  // bubbling
element.addEventListener("click", handler, true);   // capturing
event.stopPropagation();

Event Bubbling

Bubbling में, एक event target पर शुरू होता है और अपने ancestors के ज़रिए ऊपर की ओर बढ़ता है। यह DOM में default event flow है और यही वजह है कि कई containers के अंदर गहराई से nested किसी बटन पर click उन बाहरी containers पर लगे listeners को भी trigger कर सकता है।

उदाहरण: Event Bubbling

javascript
<div id="outer">
  <button id="inner">Click</button>
</div>
<script>
  // Listen for the `click` event on `document.getElementById("outer")` and run the handler when it fires
  document.getElementById("outer").addEventListener("click", () => console.log("Outer (bubbled)"));
  // Listen for the `click` event on `document.getElementById("inner")` and run the handler when it fires
  document.getElementById("inner").addEventListener("click", () => console.log("Inner (target)"));
</script>

Event Capturing

Capturing में, event bubble होकर वापस आने से पहले बाहरी ancestor से target की ओर जाता है।

Capturing listeners कम इस्तेमाल होते हैं पर addEventListener को तीसरे argument के रूप में true (या {capture: true}) पास करके register किए जाते हैं, जो target के अपने listeners के fire होने से पहले चलते हैं।

उदाहरण: Event Capturing

javascript
<div id="outer">
  <button id="inner">Click</button>
</div>
<script>
  // Listen for the `click` event on `document.getElementById("outer")` and run the handler when it fires
  document.getElementById("outer").addEventListener("click", () => console.log("Outer (capture)"), true);
  // Listen for the `click` event on `document.getElementById("inner")` and run the handler when it fires
  document.getElementById("inner").addEventListener("click", () => console.log("Inner (target)"));
</script>

Propagation रोकना

stopPropagation किसी event को दूसरे ancestors तक जारी रहने से रोकता है। किसी handler के अंदर इसे कॉल करना event को किसी भी ancestor listener तक पहुंचने से रोक देता है, जो तब उपयोगी है जब किसी nested element को किसी खास interaction का पूरी तरह मालिक होना हो।

उदाहरण: Stopping Propagation

javascript
<div id="outer">
  <button id="inner">Click</button>
</div>
<script>
  // Listen for the `click` event on `document.getElementById("outer")` and run the handler when it fires
  document.getElementById("outer").addEventListener("click", () => console.log("Never runs"));
  document.getElementById("inner").addEventListener("click", (e) => {
    // Call `e.stopPropagation()`
    e.stopPropagation();
    // Print "Inner only" to the console
    console.log("Inner only");
  });
</script>

stopImmediatePropagation

stopImmediatePropagation current event को रोक देता है और उसी target पर मौजूद दूसरे listeners को चलने से रोकता है।

यह stopPropagation से ज़्यादा targeted stop है — यह उसी exact element पर उसी event के लिए register किए गए किसी भी दूसरे listener को भी block करता है।

उदाहरण: stopImmediatePropagation

javascript
<button id="btn">Click</button>
<script>
  // Declare the constant `btn`, set to `document.getElementById("btn")`
  const btn = document.getElementById("btn");
  btn.addEventListener("click", (e) => {
    // Call `e.stopImmediatePropagation()`
    e.stopImmediatePropagation();
    // Print "First listener" to the console
    console.log("First listener");
  });
  // Listen for the `click` event on `btn` and run the handler when it fires
  btn.addEventListener("click", () => console.log("Never runs"));
</script>

Bubbling और Capturing साथ में

दोनों phases को जानना आपको यह control करने में मदद करता है कि event listeners किस क्रम में चलते हैं। साथ में, capturing और bubbling वह पूरी three-phase यात्रा (capture down, target, bubble up) define करते हैं जिससे हर DOM event गुज़रता है।

उदाहरण: Bubbling and Capturing Together

javascript
<div id="outer">
  <button id="inner">Click</button>
</div>
<script>
  // Listen for the `click` event on `document.getElementById("outer")` and run the handler when it fires
  document.getElementById("outer").addEventListener("click", () => console.log("2: capture"), true);
  // Listen for the `click` event on `document.getElementById("inner")` and run the handler when it fires
  document.getElementById("inner").addEventListener("click", () => console.log("1: target"));
  // Listen for the `click` event on `document.getElementById("outer")` and run the handler when it fires
  document.getElementById("outer").addEventListener("click", () => console.log("3: bubble"));
</script>
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. stopPropagation() कॉल करना और analytics जैसे ऊपर सुनने वाले दूसरे code को तोड़ देना।
  2. यह भूल जाना कि listeners डिफ़ॉल्ट रूप से bubbling phase में चलते हैं; capturing के लिए true या { capture: true } pass करें।
  3. event.target (जहाँ से शुरू हुआ) को event.currentTarget (जहाँ listener है) से confuse करना।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.