Event Delegation
Event delegation is one listener on a parent that handles events from many children, using bubbling and event.target (usually with closest). New children are covered automatically.
Detailed Explanation
A list of a thousand rows should not get a thousand addEventListener calls. One listener on the <ul> sees every click that bubbles from a row. You ask "which row?" with event.target.closest("li"), check that the row is still inside the list, and then read dataset.id.
Benefits: less memory, no re-bind when you add or replace rows, and a single place to remove the listener. This is the default pattern for tables, menus, and toolbars.
The parent must sit above the nodes in the bubble path. If a child calls stopPropagation, the parent never sees the event. If the event does not bubble (focus), delegation on that type fails; use focusin or bind on the child.
closest walks from the target up. A click on a <span> inside the <li> still finds the row. A click on empty padding of the <ul> returns null. Clicks on a nested widget that is not a row should be ignored: if (!row || !parent.contains(row)) return.
What it costs you: a slightly less obvious stack ("why did the list handler run?"). Filter early. Do not put a slow handler on document for every click on the page unless you mean to.
Reach for delegation when children are many or dynamic. Reach for a direct listener when one unique control has unique behaviour (a modal's close button). Reach for capture delegation only when bubble is stopped below you and you still must hear the event.
Keyboard delegation is the same pattern with keydown and a check for Enter / Space on a role=button child. If you only delegate click, keyboard users on a custom control never hit the handler. Prefer real <button> elements so the browser fires click for you.
For change on inputs, delegation on the form still works because change bubbles. focus does not; use focusin if the parent must see focus moving between fields.
Code Examples
const list = document.querySelector("#todos");
list.addEventListener("click", (event) => {
const btn = event.target.closest("[data-action]");
if (!btn || !list.contains(btn)) return;
const id = btn.closest("li")?.dataset.id;
if (btn.dataset.action === "delete" && id) {
console.log("delete", id);
}
});Adding a row later: no new listener
// Adding a row later: no new listener
const li = document.createElement("li");
li.dataset.id = "9";
li.innerHTML = `<button type="button" data-action="delete">x</button>`;
list.append(li);Common Gotchas
`event.target` can be a text node in some browsers for certain events
`closest` exists on elements; use `event.target.closest` only after ensuring it is an `Element` (`event.target.nodeType === 1` or `event.target.closest?.`).
Filter with `contains`
a `closest("li")` that matches a row *outside* this list (nested lists) is a bug if you skip the check.
Non-bubbling events cannot be delegated on the bubble phase
pick a bubbling sibling event or bind locally.
`stopPropagation` on a child disables delegation
`stopPropagation` on a child disables delegation