Bitlyst

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.

How did you like this post?

👍0
❤️0
🔥0
🤔0
😮0
Event Bubbling vs Event Capturing in JavaScript · Bitlyst