JavaScript Interview Question Explain Event Delegation
How to answer the event delegation question: one listener on a parent handles child events because they bubble. See closest, dynamic items and limits.

Event delegation is a front end interview staple, and the question usually comes in two forms: "what is it" and "you have a list with a thousand items and a button on each, how do you handle the clicks". The short answer to both is the same: attach one listener to a parent element, let events from the children bubble up to it, and use the event's target to find out which child was involved. The details that separate a good answer from a basic one are why it works, how to match the right element when the target is a nested child, what happens to items added later, and which events do not bubble. This post gives the answer in thirty seconds, then the working code with its console output, dynamic content, the target and currentTarget distinction, the limits, and the mistakes. The examples run in a browser, since they use the DOM.
The answer in thirty seconds
When you click an element, the browser fires a click event on it, and then the same event travels up through each ancestor, a step called bubbling. Delegation uses that. Instead of attaching a listener to every button in a list, you attach one to the list itself. When any button is clicked, the event bubbles up to the list, where the single handler looks at event.target to see what was clicked and acts on it. This uses less memory than many listeners, and it keeps working for elements added after the page loaded, because the listener lives on the parent, which does not change.
<ul id="todo">
<li data-id="1">Write post <button class="remove">x</button></li>
<li data-id="2">Test code <button class="remove">x</button></li>
</ul>const list = document.querySelector("#todo");
list.addEventListener("click", (event) => {
const button = event.target.closest("button.remove");
if (button === null || !list.contains(button)) return;
const item = button.closest("li");
console.log("remove", item.dataset.id);
item.remove();
});remove 2The output is what appears in the console after the second button is clicked. The listener is attached once. The handler ignores clicks that did not come from a remove button, finds the list item with closest("li"), reads its data attribute and removes it. Writing this, with the closest call, is the answer to the practical form of the question.
Why it works: the three phases
An event passes through three phases. In the capture phase it travels from the window down to the target. At the target, listeners on the element itself run. In the bubble phase it travels back up. Listeners added with addEventListener(type, handler) run in the bubble phase by default, and a third argument of true, or { capture: true }, makes them run in the capture phase instead.
const outer = document.querySelector("#todo");
const first = outer.querySelector("li");
const button = first.querySelector("button");
outer.addEventListener("click", () => console.log("ul capture"), true);
outer.addEventListener("click", () => console.log("ul bubble"));
first.addEventListener("click", () => console.log("li bubble"));
button.addEventListener("click", () => console.log("button target"));
button.click();ul capture
button target
li bubble
ul bubbleThe capture listener on the list runs first, on the way down. Then the handler on the button, the target. Then the handlers on li and ul run in turn as the event bubbles up. The delegated handler in the first example is a bubble listener on the ul, which is why it sees the event after the child has handled it. Knowing this order lets you explain why a delegated handler runs late and how to run earlier with capture.
target and currentTarget
Two properties of the event are easy to confuse, and the question about them often follows.
list.addEventListener("click", (event) => {
console.log("target:", event.target.tagName);
console.log("currentTarget:", event.currentTarget.tagName);
console.log("same:", event.target === event.currentTarget);
});target: BUTTON
currentTarget: UL
same: falseevent.target is the element where the event started, the innermost one that was clicked. event.currentTarget is the element the running listener is attached to, which is the list. In a delegated handler they differ, and the work of delegation is mapping from target to the element you care about. Inside the handler, this is the same as currentTarget for a regular function, and not for an arrow function.
Finding the right element with closest
A click often lands on a child of the element you expect, and a direct comparison of the target misses it.
<button class="buy"><span class="icon">+</span> Buy</button>document.addEventListener("click", (event) => {
console.log("target is button:", event.target.matches("button.buy"));
console.log("closest finds button:", event.target.closest("button.buy") !== null);
});target is button: false
closest finds button: trueWhen the user clicks the icon, event.target is the span, so matches("button.buy") is false and a handler written that way ignores the click. closest() walks up from the target through its ancestors and returns the first one that matches the selector, or null. Use it every time. The extra check list.contains(button) in the first example guards against a match above the delegating element, in the case where lists are nested.
Items added later
The listener lives on the parent, so new children are covered without any extra work.
const item = document.createElement("li");
item.dataset.id = "3";
item.innerHTML = 'New task <button class="remove">x</button>';
list.append(item);
item.querySelector("button").click();remove 3The new item was appended after the listener was attached, and its button works at once. With a listener on each button, the code that created the item would have to attach one as well, and forgetting it is a common bug. This is the strongest practical argument for delegation, and it is the reason frameworks and libraries use it internally.
Using data attributes for actions
A single container can handle several kinds of action by reading a data attribute.
<div id="toolbar">
<button data-action="save">Save</button>
<button data-action="delete">Delete</button>
<button data-action="share">Share</button>
</div>const actions = {
save: () => "saving",
delete: () => "deleting",
share: () => "sharing",
};
document.querySelector("#toolbar").addEventListener("click", (event) => {
const button = event.target.closest("[data-action]");
if (button === null) return;
const run = actions[button.dataset.action];
if (run) console.log(run());
});deletingThe output is for a click on the Delete button. One listener handles the whole toolbar, and adding a new button is a matter of adding an element and an entry in the actions object. The lookup in a plain object relies on button.dataset.action being under your control. If the value came from user content, use a Map or check Object.hasOwn(actions, name), so a name like constructor does not resolve to something inherited.
Events that do not bubble
Delegation depends on bubbling, and not every event bubbles.
| Event | Bubbles | Delegation alternative |
|---|---|---|
click, input, change, keydown, submit | Yes | Use directly |
focus, blur | No | Use focusin and focusout, which bubble |
mouseenter, mouseleave | No | Use mouseover and mouseout |
scroll on an element | No | Attach to the element, or capture on an ancestor |
load, error on images | No | Capture on an ancestor |
const form = document.querySelector("form");
form.addEventListener("focus", () => console.log("focus: never fires here"));
form.addEventListener("focusin", (event) => console.log("focusin:", event.target.name));
form.querySelector("input").focus();focusin: emailA listener for focus on the form does not run when an input inside it gains focus, because focus does not bubble. The focusin event is the same signal in a bubbling form. The capture phase is the other workaround: a capturing listener on an ancestor sees events from children whether or not they bubble, because capture happens on the way down.
When delegation fails
Two things stop it. If an event does not bubble, there is nothing to catch. And if some code between the target and your parent calls event.stopPropagation(), the event never reaches the delegating element.
const stopper = list.querySelector("button");
stopper.addEventListener("click", (event) => event.stopPropagation());
stopper.click();
console.log("done");doneNothing was logged by the delegated handler, because the button's own listener stopped the event. This is a real source of bugs when a component library stops propagation to protect itself, and a delegated analytics or menu handler stops working for those elements. Prefer event.preventDefault() to stop browser behavior and avoid stopPropagation() unless the event must not be seen by ancestors.
Testing and synthetic events
const plain = new Event("click");
const bubbling = new Event("click", { bubbles: true });
const inner = list.querySelector("button");
inner.dispatchEvent(plain);
console.log("after non-bubbling event");
inner.dispatchEvent(bubbling);
console.log("after bubbling event");after non-bubbling event
remove 1
after bubbling eventAn event created with new Event("click") does not bubble unless you pass { bubbles: true }, so the delegated handler did not see the first one. The second did, and removed item 1. Tests that dispatch events by hand have to set bubbles for delegation to work, and element.click() creates a bubbling event, which is why it is the easier choice.
A mistake we hit: delegating on document for everything
document.addEventListener("click", (event) => {
const link = event.target.closest("a");
if (link) {
console.log("link clicked, default prevented:", event.defaultPrevented);
}
});link clicked, default prevented: falseA single document listener for every click on the page works and invites trouble: it runs for every click, it competes with the handlers of other components, and it sees an event after those handlers have had their chance to call preventDefault(). A delegate should sit on the nearest stable container, such as the list or the toolbar, and not on document. We moved a global handler to the container it belonged to, and a bug where two components handled the same click disappeared.
Wrong answers that lose the point
"Delegation attaches the listener to each child more efficiently." It attaches nothing to the children.
"It works for every event." Only for events that bubble.
"
event.targetis the element with the listener." That iscurrentTarget;targetis where the event started."Delegation is only an optimization." Handling items added later is just as important.
Choosing between the approaches
| You have | Use |
|---|---|
| A list with many items, or items added later | Delegation on the list |
| One or two fixed buttons | A listener on each |
| A focus or hover event | focusin, focusout, mouseover or mouseout |
| An event that does not bubble | A listener on the element, or capture on an ancestor |
| Several actions in one container | data-action attributes and a lookup |
| Framework components | The framework's own event handling |
Practical rules we follow
Delegate on the nearest stable container, never on document by habit. Use closest() and check contains(). Read behavior from data- attributes, and validate names against an allow-list. Avoid stopPropagation(). Remember which events bubble. Pass { bubbles: true } in tests. And keep the handler short, dispatching to named functions.
Where the event delegation answer stops being enough
A deeper question covers shadow DOM, where events are retargeted so that event.target at the host is the host, and composedPath() gives the real path. It may cover passive listeners, AbortController for removing listeners, and how frameworks implement a synthetic event system. The reference is in the MDN event bubbling guide and the DOM specification on event dispatch.


