Skip to content
Velloreth
Web Development 4 min read

Event Delegation Explained: One Listener for Many Elements

Attach one listener to a parent instead of hundreds to children. Learn the bubbling, the target, and the pitfalls to avoid.

By

The problem

A list with a listener per row.

document.querySelectorAll('.row').forEach((row) => {
  row.addEventListener('click', handleClick);
});

With a thousand rows, that is a thousand listeners.

And a row added later has no listener.

The solution

One listener on the parent.

document.querySelector('.list').addEventListener('click', (event) => {
  const row = event.target.closest('.row');
  if (!row) return;
  handleClick(row);
});

One listener, and it works for a row added later.

How it works

The event bubbles from the target up through the ancestors.

click on a button
  -> the button
  -> the row
  -> the list
  -> the body
  -> the document

The listener on the list catches the event as it passes.

event.target is the element that was clicked, which may be a child of the row.

closest('.row') walks up to find the row.

The target

event.target is the deepest element.

<li class="row"><button>Delete</button></li>

A click on the button makes event.target the button, not the row.

closest('.row') finds the row.

event.currentTarget is the element the listener is attached to, which is the list.

The distinction matters. Using event.target directly is the common bug.

The pattern

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

  const action = button.dataset.action;
  const row = button.closest('.row');

  switch (action) {
    case 'delete': deleteRow(row); break;
    case 'edit':   editRow(row);   break;
  }
});

A data attribute names the action, which avoids a class check per button.

One listener handles every action in the list.

Why it is faster

One listener instead of hundreds.

Per elementDelegation
Listenersone per elementone
Memoryproportionalconstant
Setupa loopone call
A new elementattach a listenernothing

The runtime cost is similar, because the event fires once per interaction either way.

The saving is largest for a long list, where a listener per row is a listener per row of memory.

The events that do not bubble

Does not bubbleBubbling equivalent
focusfocusin
blurfocusout
mouseentermouseover
mouseleavemouseout
load—
error—

For a non-bubbling event, attach the listener to the element itself, or use the capture phase.

list.addEventListener('focusin', handler);   // bubbles

The capture phase

The event travels down from the window to the target, then bubbles up.

list.addEventListener('click', handler, { capture: true });

A capture listener runs before the target’s own listener.

Use it to intercept an event before a child handles it, or for a non-bubbling event.

The pitfalls

event.target instead of closest(). The target may be a child.

A non-bubbling event. The listener never fires.

stopPropagation in a child. The event does not reach the parent.

A selector that matches too much. closest('div') finds the nearest div, which may not be the one intended.

A listener on the document for everything. It catches events from unrelated parts of the page.

A removed element. The listener on the parent remains, which is the point, but the handler must handle a missing element.

A shadow DOM boundary. An event from inside a shadow root is retargeted, so event.target is the host.

A framework that manages the DOM. A framework’s own event system may not use the native bubbling.

Common mistakes

event.target without closest(). The wrong element.

A non-bubbling event. No delegation.

stopPropagation in a child. The parent never sees the event.

A listener on the document. It catches unrelated events.

A selector that is too broad. The wrong ancestor.

No null check. closest() returns null.

A shadow DOM boundary. The target is retargeted.

A framework’s event system. The native bubbling may not apply.

A listener that is never removed. A leak on a long-lived page.

A quotable summary

Event delegation attaches one listener to a parent instead of one per child, and it relies on event bubbling so the event reaches the parent. Use event.target.closest(selector) to find the element that matters, because the target is the deepest element and may be a child. It works for elements added later and uses less memory, and the events that do not bubble, such as focus and blur, need the bubbling equivalent or the capture phase.

For the related topics, see javascript-dom-manipulation and javascript-array-methods-guide. For the broader context, javascript-performance-tips and javascript-debugging-console.

Frequently asked questions

What is event delegation?
It is attaching a single event listener to a parent element and using the event target to decide which child was involved. It replaces one listener per child, which is faster and works for children added later. It relies on event bubbling.
Why is event delegation faster?
Because one listener is attached instead of hundreds, which uses less memory and less setup time. The saving is largest for a long list, where a listener per row is a listener per row of memory. The event still fires once per interaction, so the runtime cost is similar.
What is event bubbling?
It is the propagation of an event from the target up through its ancestors. A click on a button inside a div fires on the button, then the div, then the body, then the document. Delegation uses this to catch the event at a parent.
What is closest() used for?
It walks up from the event target to find the nearest ancestor that matches a selector. In delegation, it identifies the element that matters, such as the list item that contains the clicked button. It returns null if no ancestor matches.
Which events do not bubble?
focus, blur, mouseenter, mouseleave, load and error do not bubble. The bubbling equivalents are focusin, focusout and mouseover, mouseout. For a non-bubbling event, attach the listener to the element itself or use the capture phase.
What is the capture phase?
The phase before the target, where the event travels down from the window to the target. A listener with capture: true runs in this phase. It is used for a non-bubbling event or to intercept an event before a child handles it.