JavaScript Event Delegation: Handle Clicks on Dynamic Content

Learn how event delegation uses bubbling and event.target.closest() to handle child interactions, including elements added later.

JavaScript Event Delegation: Handle Clicks on Dynamic Content

What is Event Delegation, and Why is it useful for Dynamic Content?

Event delegation means listening for an event on a shared parent instead of adding a separate listener to every child. When an event such as click bubbles up from a child, the parent listener can inspect the event and decide which child initiated it.

This is especially useful for dynamic content. If new buttons are added under the same parent later, their clicks bubble to the existing parent listener. You do not need to attach another listener to each new button. MDN: Event bubbling and delegation

A list with remove buttons

Suppose a page has a task list. Each remove button includes a nested <span> for its icon:

<ul id="task-list">
  <li>
    Review JavaScript
    <button data-action="remove"><span>×</span> Remove</button>
  </li>
  <li>
    Practice DOM events
    <button data-action="remove"><span>×</span> Remove</button>
  </li>
</ul>

Instead of listening to every button, attach one listener to the list:

const taskList = document.querySelector("#task-list");

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

  const removeButton = event.target.closest('[data-action="remove"]');

  if (!removeButton || !taskList.contains(removeButton)) return;

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

Here is what happens:

  1. The user clicks the button or its nested <span>.
  2. The click bubbles up to taskList, where the one listener runs.
  3. event.target identifies the element where the click started. closest() walks up from that element to find the remove button, even if the span was clicked.
  4. The containment check ensures the match belongs to this list. Then we find and remove its <li>.

event.currentTarget would be the <ul> because that is where this handler is attached. event.target is the clicked element or its nested child. MDN: target · MDN: currentTarget · MDN: closest()

It also handles elements added later

Add a new task after the delegated listener has been set up:

const newTask = document.createElement("li");
newTask.innerHTML =
  'Write a test <button data-action="remove"><span>×</span> Remove</button>';

taskList.append(newTask);

The new remove button works with the existing list listener because its click bubbles to taskList. This example uses a fixed, trusted string. If the text comes from a user, avoid inserting it with innerHTML; use textContent and create the button with DOM methods instead.

Use the nearest stable parent

Delegate from a parent that contains the relevant children and will stay in the document, such as the list itself. Attaching everything to document can make handlers less focused and easier to collide with unrelated elements. Use a selector or data-* attribute that clearly identifies the action.

When delegation may not work as expected

  • Delegation relies on an event reaching the parent. Some events do not bubble; for focus behavior, focusin and focusout bubble where focus and blur do not. Check the event you plan to delegate.
  • A child handler that calls event.stopPropagation() can prevent the event from reaching the delegated listener.
  • Always verify the matched element belongs to the parent you are handling, especially when using broad selectors or nested lists.
  • Delegation reduces the number of attached listeners and simplifies dynamic child handling, but it is not automatically faster for every small interface.

Try it yourself

Attach one delegated click listener to a <ul> and handle buttons with data-action="done". Use closest() so clicking a nested icon also marks the correct list item as complete. Then add another list item dynamically and confirm it works without registering another listener.

Recap

  • Event delegation puts one listener on a shared parent.
  • Bubbling carries child events up to that parent.
  • event.target.closest() helps find which relevant child was involved.
  • New matching children work with the existing listener when added under that parent.
  • Delegation needs a bubbling event and careful selector/containment checks.

What's next?

Next, we’ll compare event bubbling with capturing and learn how to stop propagation.

Written by

Dev Likhon

Dev Likhon

Full Stack Developer & Tech Educator

6+ years of professional development experience.

About Me
Share:

Comments

Share what you learned or ask a question.

Loading comments...