Event Delegation in JavaScript and When to Use It
You render a list of 300 rows, attach a click listener to each delete button, and everything works until new rows arrive from the server and their buttons do nothing.

You render a list of 300 rows, attach a click listener to each delete button, and everything works until new rows arrive from the server and their buttons do nothing. Then you add listeners after every render, and a week later someone finds a row where one click deletes twice. Event delegation solves both problems with one listener on a parent element, but only if you understand how it actually decides which element was clicked.
How event delegation works
Most DOM events bubble. When you click a button inside a list item inside a ul, the browser fires the click on the button first, then on the li, then on the ul, and so on up to the document. Event delegation takes advantage of that. Instead of listening on every button, you listen once on a stable ancestor and work out which descendant started the event.
The key property is event.target, which is the deepest element that received the click. The key method is closest, which walks up from that element to find the nearest ancestor matching a selector. Together they look like this:
list.addEventListener('click', function (event) { const button = event.target.closest('button[data-action]'); if (!button || !list.contains(button)) return; handleAction(button.dataset.action, button); });
Three details in those few lines matter more than they look:
- closest, not target directly. If the button contains an icon span or an SVG, event.target is often the icon, not the button. Checking target.matches('button') fails for every click that lands on the icon, which is usually most of them.
- The contains check. closest walks all the way up the tree, so it can match a button outside your list if the list itself sits inside one. Checking that the container still contains the match keeps the handler honest.
- Data attributes for intent. Reading data-action, such as delete, edit or archive, lets one handler serve several buttons without a chain of class name checks.
When event delegation is the right tool
Delegation earns its place in a few clear situations:
- Dynamic content. Rows added by infinite scroll, chat messages, search results or anything rendered after page load work automatically, because the listener lives on a parent that never changes.
- Large repeated lists. A table with 1,000 rows and three buttons each means 3,000 listeners without delegation, and one with it. Memory use is small either way on a modern laptop, but setup time and cleanup on a low end phone are noticeably better with one.
- Uniform actions. Toolbars, tab lists, card grids and menus where every item does a variation of the same thing.
- Server rendered pages. When HTML arrives from the back end with no client framework, delegation lets a small script add behaviour to everything at once.
Skip it when there is one element with one job, such as a single submit button or a dialog close icon. A direct listener is easier to read and to find with dev tools. Skip it too for high frequency events on one element, like scroll or pointermove on a canvas, where the extra closest call on every event buys you nothing.
If the timing of when handlers run puzzles you, our explainer on how the browser event loop decides when your code runs covers how click events are queued and dispatched.
Events that do not bubble, and what to use instead
Delegation only works for events that bubble, and several common ones do not.
| Event that does not bubble | Bubbling alternative | Typical use |
|---|---|---|
| focus | focusin | Highlighting the active form field |
| blur | focusout | Validating a field when the user leaves it |
| mouseenter | mouseover | Hover previews on list items |
| mouseleave | mouseout | Hiding a preview |
| load and error on images | Capture phase listener | Swapping broken thumbnails |
For the image case, pass true or an options object with capture set to true as the third argument of addEventListener. Capture listeners run on the way down the tree, so the parent sees the event even though it never bubbles back up.
The mouseover alternative has a catch: it fires again every time the pointer crosses into a child element. Compare event.relatedTarget against the item you care about, or debounce the work as described in our guide to debounce vs throttle in JavaScript.
Why "always delegate to document" is bad advice
Some older articles, and plenty of jQuery era code, recommend attaching every delegated handler to document so nothing is ever missed. I would push back on that for three reasons.
First, every click anywhere on the page now runs every handler, each calling closest and returning early. With 20 delegated handlers on document, a single click on a blank area triggers 20 functions doing pointless work. It is rarely slow enough to measure, but it makes performance profiles noisy and hard to read.
Second, document level handlers are fragile. Any component that calls stopPropagation, such as a dropdown or a modal library closing itself on outside clicks, silently stops your handler from ever seeing the event. You get bug reports that only happen when a menu is open, and they take hours to trace.
Third, ownership gets lost. When a component is removed, its handler on document lives on, and a team six months later has no idea which handlers are still needed.
The better rule is to delegate to the nearest stable container that the component owns: the ul for a list, the table body for a table, the form for a form. You keep the benefits of delegation, and the handler disappears when the container does.
Accessibility and other common mistakes
Delegation does not change accessibility rules, but it makes some mistakes easier to hide.
- Delegating clicks on non buttons. A click handler on a div with data-action works for mouse users and fails for keyboard users. Use real button elements, which fire click on Enter and Space for free.
- Forgetting disabled states. A disabled button does not fire click, but a span inside a styled fake button will. Check button.disabled or aria-disabled in the handler if you use custom controls.
- Calling preventDefault too broadly. A delegated handler on a form that prevents default on every click will block links and checkboxes inside it. Only prevent default after you have matched the element you mean to handle.
- Relying on event.currentTarget inside nested helpers. currentTarget is the element the listener is attached to and is only valid during dispatch. Store what you need before any await.
Delegation also pairs well with CSS for state. Toggle a class or an attribute in the handler and let styles do the rest, an approach covered in what modern CSS can do now that used to need JavaScript.
Frequently asked questions
Is event delegation faster than individual listeners?
For large or frequently changing lists, it usually is, mainly because you set up and clean up one listener instead of hundreds. For a handful of static buttons, the difference is negligible. Choose it for maintainability first and speed second.
Does event delegation work with React or Vue?
Both frameworks already delegate internally, so you rarely need to do it by hand inside components. It is still useful for plain HTML regions, third party widgets or content injected outside the framework's control.
Why does event.target return the wrong element?
event.target is the deepest element clicked, which is often an icon or a span inside your button. Use event.target.closest with a selector for the element you want, and it walks up to the right one.
More in Front-End
Front-End
How to Find the Element Causing Horizontal Scroll
A tester filed a bug last March that said "page wobbles sideways on my phone". No screenshot, no device model.
Front-End
How Front-End Retry Code Quietly Overloads Your API
A payments endpoint we run went dark for about 90 seconds on a Tuesday afternoon.
Front-End
Why z-index Stops Working: Stacking Contexts Explained
The dropdown menu opens, and half of it disappears behind the hero image below. You give the menu z-index: 10 . Nothing.