# Bubbling, Capturing & Delegation

> Understand how events travel through the DOM tree. Learn how to use Event Delegation to write highly performant, memory-efficient listeners.

Subject: JavaScript | Lesson 44 of 63 | 6 min read
Interactive lesson: https://codewithanimation.in/javascript/event-bubbling-delegation
Previous: [Events & Listeners](https://codewithanimation.in/javascript/events-and-listeners.md) | Next: [Forms & User Input](https://codewithanimation.in/javascript/forms-and-input.md)

## The Event Ripple

When you click a `<button>` that is nested inside a `<div>`, you aren't just clicking the button. Technically, you are also clicking the `<div>`, the `<body>`, and the entire `document`.

The browser handles this using a process called **Event Bubbling**. When an event fires, it triggers the listener on the innermost target element first, and then "bubbles" upwards through the ancestors, triggering their listeners one by one.

## Event Delegation

Bubbling is incredibly useful because it enables a pattern called **Event Delegation**.

Imagine a shopping cart with 100 'Delete' buttons. Attaching 100 individual event listeners consumes massive amounts of browser memory. Instead, you can attach exactly **one** event listener to the parent `<ul>` container.

When a user clicks a 'Delete' button, the click event bubbles up to the `<ul>`. Inside the `<ul>`'s listener, you inspect `e.target` to figure out which specific button caused the ripple!

## Implementing Delegation

Here is how you write a delegated listener:

```javascript
document.getElementById('cart-list').addEventListener('click', (e) => {
  if (e.target.classList.contains('delete-btn')) {
    e.target.parentElement.remove();
  }
});
```

Notice how we check `e.target` before acting. This ensures we only run our logic if the user clicked the button, not if they clicked the empty space inside the `<ul>`.

## Common mistake: Stopping the Ripple

Sometimes, bubbling is bad. For example, clicking a 'Like' button on a post might trigger the 'Like' action, but bubble up and accidentally trigger a click listener on the Post card that redirects the user to a new page.

You can stop the bubbling process immediately by calling `e.stopPropagation()` inside the child's event listener.

## Tip: The Capturing Phase

Before an event bubbles UP, it actually trickles DOWN from the document to the target element. This is called the 'Capturing Phase'. It is rarely used, but you can listen during this phase by passing `{ capture: true }` as a third argument to `addEventListener`.

## Try it: Visualizing Delegation

Study the architecture diagram below. It illustrates why placing a single listener on an ancestor node is vastly superior to placing listeners on every child.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/event-bubbling-delegation

## Check yourself

### Question 1

What is Event Bubbling?

- A. The process where an event triggers on the innermost element, and then propagates upwards to its ancestors.
- B. The process where an event starts at the document root and propagates downwards to the target.
- C. The process of deleting old event listeners from memory.

Correct answer: A

Bubbling is the default behavior for most events, rippling upwards like a bubble rising in water.

- Why not B: Incorrect. That is the Capturing phase.
- Why not C: Incorrect.

### Question 2

Why is Event Delegation preferred for dynamic lists (like a to-do list)?

- A. Because you only need one listener on the parent, which saves memory and automatically works for new items added later.
- B. Because it makes the events bubble faster.
- C. Because it stops malicious XSS attacks.

Correct answer: A

If you add a new `<li>` to the DOM later, you don't have to manually attach a new listener to it. The single parent listener will automatically catch its bubbles!

- Why not B: Incorrect. It doesn't change the speed of bubbling.
- Why not C: Incorrect. Event delegation is for performance and architecture, not security.

### Question 3

How do you prevent an event from bubbling up to parent elements?

- A. Call e.stopPropagation() inside the event handler.
- B. Call e.preventDefault() inside the event handler.
- C. Return false from the event handler.

Correct answer: A

`e.stopPropagation()` immediately halts the bubbling process, ensuring no parent listeners are triggered by the interaction.

- Why not B: Incorrect. preventDefault stops default HTML behaviors (like links routing), but the event still bubbles.
- Why not C: Incorrect. While this works in older jQuery architectures, standard JS requires e.stopPropagation().

## Remember this

- Events 'bubble' upwards from the target element all the way to the document root.
- Use `e.stopPropagation()` to stop the event from bubbling further.
- Event Delegation involves placing a single listener on a parent element to handle events from its children.
- Delegation is highly memory-efficient and automatically handles newly injected DOM elements.
- Inside a delegated listener, check `e.target` to verify exactly what was clicked before acting.
