Skip to content
Browser & DOMChapter 39 / 47Intermediate4 min read

Event Bubbling & Delegation

Learn JavaScript event delegation and bubbling, the capture phase, event.target and closest(), and handle many items with one parent listener.

On this page

1. What are event bubbling and delegation?

When you click a button, that click does not belong only to the button. The event "bubbles" from the button to its parent, then that parent's parent, and finally up to document.

Event delegation is a pattern: instead of adding a listener to every child, add one listener to the common parent and use event.target to find out which child was actually clicked.

2. Why does it matter?

If a list has 500 items, 500 listeners use memory. And items added later do not have a listener at all.

Listener on every itemEvent delegation
500 items, 500 listenersJust 1 listener on the parent
Attach listeners to new items yourselfNew items are handled automatically
Clean up listeners when removing itemsNo cleanup needed
Setup code scattered aroundAll the logic in one place

3. Where is it used?

PlaceExample
Todo listDelete and done buttons in every item
TablesClick a row to open its details
Menus / tabsWhich tab was clicked
Infinite scroll feedsLike buttons on new posts
Closing a dropdownListen for clicks on document to detect an outside click

4. How does it work?

An event has three phases:

Event phases
Capture phase: from window down to the target
Target phase: on the actual element
Bubble phase: from the target up to window

By default, addEventListener listens in the bubble phase. To listen in the capture phase, pass { capture: true }.

Delegation flow
The user clicks the Delete button inside an li
The event bubbles up to the ul
The ul listener looks at event.target
closest finds the right button and li
Only that item is updated

5. Code example

HTML:

html
<ul id="todos">
  <li data-id="1">Study <button data-action="delete">Delete</button></li>
  <li data-id="2">Walk <button data-action="done">Done</button></li>
</ul>

JavaScript:

js
const list = document.querySelector("#todos");

list.addEventListener("click", (event) => {
  const button = event.target.closest("button[data-action]");
  if (!button || !list.contains(button)) return;

  const item = button.closest("li");
  const { action } = button.dataset;

  if (action === "delete") item.remove();
  if (action === "done") item.classList.toggle("done");

  console.log(`Item ${item.dataset.id}: ${action}`);
});

const li = document.createElement("li");
li.dataset.id = "3";
li.innerHTML = `Groceries <button data-action="delete">Delete</button>`;
list.append(li);

How does the code work?

LineWhat happens
list.addEventListener("click", ...)Just one listener on the parent ul
event.target.closest(...)You find the button even if the click lands on any part inside it
if (!button ...) returnIgnores empty space and buttons outside the list
button.dataset.actiondata-action tells you what to do
The new liNo new listener is added, and it still works

Interview tip

focus, blur, mouseenter and mouseleave do not bubble. For delegation, use their bubbling counterparts: focusin, focusout, mouseover and mouseout.

6. Common mistakes

Mistake: Assuming event.target is the button itself

Why? If the button contains an icon or span, the target is that inner element and the dataset comes back empty.

Better: Use event.target.closest('button') to find the right element.

Mistake: Forgetting the null check

Why? If the click lands on empty space in the parent, closest returns null and a TypeError follows.

Better: Add a guard with if (!button) return first.

Mistake: Calling stopPropagation for no reason

Why? The event never reaches delegated listeners higher up, and features break.

Better: Check the target and decide, instead of stopping propagation.

7. What to learn before and after

Suggested order
DOM Basics
DOM Events
Bubbling and Delegation
Forms and Validation

Final quick revision

PointDetail
BubblingThe event travels from the target up to its parents
CapturingTop to bottom, listen with { capture: true }
DelegationOne listener on the parent handles the children
event.targetThe element that was actually clicked
closest()Finds a matching element going up from the target
BenefitFewer listeners, dynamic items covered automatically