
Learn how event delegation in JavaScript lets you use a single event listener to handle clicks on multiple child elements efficiently—ideal for dynamic UIs and better performance.
Martin Ferret
July 14, 2025
Have you ever added dozens of click event listeners to buttons inside a list, only to realize there might be a better way?
That better way is called event delegation, and it can save you time, memory, and complexity.
Event delegation is a technique where you add a single event listener to a parent element, and use it to manage events triggered by its child elements.
Instead of writing:
      buttons.forEach(btn => {
  btn.addEventListener('click', handleClick);
});
    You write:
      document.querySelector('#list').addEventListener('click', handleClick);
    This works because of event bubbling — when an event is triggered on a child element, it bubbles up through its ancestors.
| Scenario | Event Delegation Advantage | 
|---|---|
| Multiple similar elements | Use one listener on the parent | 
| Dynamically added elements | No need to reattach listeners | 
| Performance optimization | Fewer event listeners = better memory | 
| Cleaner code | Centralized logic, easier to maintain | 
      <div id="todo-list">
  <button data-id="1">Delete Task 1</button>
  <button data-id="2">Delete Task 2</button>
  <button data-id="3">Delete Task 3</button>
</div>
          const list = document.getElementById('todo-list');
list.addEventListener('click', function (event) {
  if (event.target.tagName === 'BUTTON') {
    const taskId = event.target.dataset.id;
    console.log(`Deleting task ${taskId}`);
  }
});
    When you click any button, the console will show:
      Deleting task 1
Deleting task 2
Deleting task 3
    Only one event listener is used, and it works for all buttons.
Suppose you add a new button dynamically:
      const newButton = document.createElement('button');
newButton.textContent = 'Delete Task 4';
newButton.dataset.id = '4';
list.appendChild(newButton);
    🔥 You do not need to add a new event listener. The existing listener on the parent will still handle the event. This is one of the main benefits of event delegation.
Event delegation relies on two key mechanisms:
event.target to determine which child element triggered the event.Example:
      list.addEventListener('click', function (event) {
  if (event.target.matches('button[data-id]')) {
    const id = event.target.dataset.id;
    // Perform action
  }
});
    You can also use event.target.closest() if you're dealing with nested elements.
input, blur)Get the latest news and updates on developer certifications. Content is updated regularly, so please make sure to bookmark this page or sign up to get the latest content directly in your inbox.

Building Reusable Components with React 19 Actions
Build reusable React components with React 19 Actions using useTransition() and useOptimistic(). Learn how to track pending states, implement optimistic updates, and expose action properties for custom logic in the Next.js App Router with practical examples.
Aurora Scharff
Oct 28, 2025

Falling with Style
Learn how Vue.js fall-through attributes work, when they're useful, and common pitfalls to avoid. Master class, style, and event handling in components.
Abdel Awad
Oct 28, 2025

Use Lighthouse to improve your Angular applications
Angular developers often focus on code structure and framework mastery—but end users care most about speed, accessibility, and visibility. This article highlights how tools like Google Chrome’s built-in Lighthouse can help you measure and improve your app’s performance, accessibility, and SEO. By running quick audits and reviewing actionable insights, developers can bridge the gap between technical excellence and real-world user experience.
Alain Chautard
Oct 24, 2025
 We can help you recruit Certified Developers for your organization or project. The team has helped many customers employ suitable resources from a pool of 100s of qualified Developers. 
 Let us help you get the resources you need. 
