Sniply Blog

Event Bubbling vs Event Capturing in JavaScript

When you click on an element inside nested elements, the event doesn’t just happen on that element β€” it travels through the DOM. There are two ways this can happen: capturing and bubbling.


1. Event Capturing (trickle down)


2. Event Bubbling (bubble up)


3. Default in JavaScript

// Bubbling (default)
button.addEventListener("click", () => {
  console.log("Button clicked!");
});

// Capturing
div.addEventListener(
  "click",
  () => {
    console.log("DIV clicked during capturing!");
  },
  true // πŸ‘ˆ capturing enabled
);

4. Event Flow Order

When you click the button:

  1. Capturing phase: document β†’ body β†’ div β†’ button
  2. Target phase: the button itself
  3. Bubbling phase: button β†’ div β†’ body β†’ document

5. Why does this matter?


🧠 Quick Mental Model


⚑ That’s it! Now you know how events travel in JavaScript.

Event Bubbling vs Event Capturing in JavaScript Β· Sniply