Skip to content
IT glossary
FrontendUpdated December 3, 2026

Event delegation

Делегирование событий

A single listener on a shared container handles events from multiple elements inside it. The listener checks which element the event came from and decides what to do.

Also known as: всплытие событий, event bubbling

What it is

Suppose a task list has a Remove button on every row. You could attach a click listener to each button, or attach one listener to the list and let it work out which button was clicked. The second approach is event delegation. It works for rows added later because the listener belongs to the list, not to any particular row.

Why the container sees the click

A DOM click travels through ancestors toward its target during the capture phase. After reaching the target, it travels back through the ancestors during the bubble phase. A listener added to the container with addEventListener('click', handler) runs on that return trip when a descendant is clicked. Bubbling is how the browser delivers the event; delegation is how you choose to handle it.

Inside the listener, event.currentTarget is the container that owns the listener. event.target is the event's target, which might be an icon inside a button rather than the button itself. Use closest() to check the target and walk up its ancestors until you find a matching control.

Example: remove an item from a list

Assume #tasks is a list whose direct li children contain buttons marked with data-remove.

const tasks = document.querySelector('#tasks');

tasks.addEventListener('click', (event) => {
  if (!(event.target instanceof Element)) return;

  const button = event.target.closest('button[data-remove]');
  if (!button || !tasks.contains(button)) return;

  const item = button.closest('li');
  if (item?.parentElement === tasks) item.remove();
});

Clicking an icon inside the button still finds the button. Clicking an unrelated part of the list does nothing. The containment check matters because closest() can search beyond the container, and the final check limits removal to rows that are direct children of this list.

When to use it and what to watch for

Delegation is useful for tables, menus, and other collections whose contents change. Choose a nearby container that stays in the DOM: replacing its children is fine, but replacing the container itself means the new node needs its own listener. Fewer listeners do not automatically make the page faster; the container also receives clicks that the handler has to filter out. For a single fixed control, a direct listener may be clearer.

Not every event bubbles. For focus changes, use the bubbling focusin event, or listen for focus during capture. A descendant listener that calls stopPropagation() can keep a click from reaching the container's bubble listener. By contrast, preventDefault() cancels a cancelable default action, such as following a link, without stopping propagation.

In a frontend interview

You may be asked to handle clicks on rows that are loaded after the page starts. Walk through a click on a nested icon: identify target and currentTarget, explain why checking target.matches('button') would miss that click, and show how the same container listener handles newly inserted rows.