Learning Tips | Published Nov 16, 2026 | 7 min read

JavaScript Event Bubbling and Delegation

Learn DOM event bubbling, capture, target vs currentTarget, and a safe delegation pattern for dynamic JavaScript interfaces.

Diagram showing a click travel down the DOM capture path to a button and bubble up to a parent list listener that handles the delegated action

JavaScript event delegation lets one listener on a stable parent handle events from many child controls, including controls added later. It works because many DOM events bubble from their target toward ancestor elements. This guide explains the event path, the difference between target and currentTarget, and a safe pattern for delegated clicks.

How a DOM event moves

When a user clicks a button nested inside a list, the browser dispatches a click event along the DOM event path. During the capture phase, it travels from ancestors toward the target. At the target, listeners on the clicked element can run. If the event bubbles, it then travels back up through the ancestors.

A listener registered with addEventListener() normally listens during the bubbling phase. Pass { capture: true } to listen during capture. For most interface actions, bubbling is the simpler default.

Read target and currentTarget correctly

  • event.target is the element where the event originated. It may be a child inside the control the user intended to activate.
  • event.currentTarget is the element whose listener is running now.

For example, clicking an icon inside a button can make the icon the target, while the button is the current target for a listener attached to that button. A delegated listener is attached to a parent, so its current target is that parent; use the target and a selector such as closest() to identify which child control was activated.

Delegate actions from a stable list

Suppose a task list has a remove button in each row. Attaching a separate listener to every button takes extra work, and a button added after setup would have no listener. A listener on the list itself can handle both existing and future rows:

const list = document.querySelector("#tasks");

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

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

  button.closest("li")?.remove();
});

The selector makes the action explicit, closest() handles clicks on nested button content, and list.contains(button) ensures the match belongs to this list. Use a real <button> for an action: it already supports keyboard activation and exposes the right control semantics to assistive technology.

Why delegation helps with dynamic content

After the list listener is registered, JavaScript can append another row with a matching remove button. The new button does not need its own listener. When it is clicked, the event bubbles to the existing list listener, which inspects the event target and finds the matching action. Delegation is especially useful for tables, menus, search results, and other collections whose rows change over time.

Choose a parent that remains in the document and is close enough to the controls to keep the handler easy to reason about. A single listener on document can work, but it may catch unrelated controls and makes event ownership less clear.

When to use capture and when events do not bubble

Use capture only when the listener needs to run on the way down the event path, before target and bubbling listeners. Most click delegation does not need it. Some event types, including focus and blur, do not bubble in the usual way; use bubbling alternatives such as focusin and focusout, or register a capture listener where appropriate. Check the event type before assuming delegation will work.

Do not stop propagation by default

stopPropagation() prevents the event from continuing to other elements along the path. It is not required just to make a parent listener handle a child action. It can also break unrelated components that depend on the same event. preventDefault() has a different job: it cancels a browser default action, such as following a link, but does not stop propagation.

Common implementation mistakes

  1. Comparing event.target directly with a button when users can click an icon or span inside it. Find the closest matching control instead.
  2. Matching a selector without checking that the matched element is inside the intended parent.
  3. Assuming every event bubbles. Confirm the behavior for the event type you want to delegate.
  4. Using propagation controls to hide duplicate handlers instead of fixing where listeners are registered.
  5. Delegating from an overly broad ancestor, which can make unrelated interactions trigger the same code.

Practical checklist

  • Attach the listener to a stable parent near the changing controls.
  • Use event.target to locate the originating control and currentTarget to understand which listener is running.
  • Use closest() and confirm containment before performing an action.
  • Keep controls semantic and check whether the event bubbles.
  • Reserve capture, stopPropagation(), and preventDefault() for cases where their specific behavior is needed.

Event delegation is a small pattern with a clear rule: let a bubbling event reach a stable ancestor, identify the intended descendant, then perform only that descendant's action. This keeps dynamic interfaces easier to maintain without hiding how events travel.

Further reading

Want a structured Java learning path?

Explore Jaipur Engineers course options to connect Java fundamentals, debugging, backend development and project practice in a guided learning plan.

Explore the Java program   Ask for course guidance