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 Velloreth
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 element | Delegation | |
|---|---|---|
| Listeners | one per element | one |
| Memory | proportional | constant |
| Setup | a loop | one call |
| A new element | attach a listener | nothing |
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 bubble | Bubbling equivalent |
|---|---|
focus | focusin |
blur | focusout |
mouseenter | mouseover |
mouseleave | mouseout |
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.
Related reading
For the related topics, see javascript-dom-manipulation and javascript-array-methods-guide. For the broader context, javascript-performance-tips and javascript-debugging-console.