JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsjavascript
PrevNext

Learn the concept

Events

javascript
junior
events

What is event delegation in JavaScript and why is it useful?

events
event-delegation
dom
event-bubbling
performance
Quick Answer

Event delegation is a pattern where you attach a single event listener to a parent element instead of individual listeners on each child. It works because events bubble up through the DOM, and you can check event.target to determine which child was clicked.

Detailed Explanation

Event delegation leverages event bubbling — when an event occurs on an element, it bubbles up through all its ancestors.

How it works:

  1. Attach one event listener to a common parent element
  2. When a child element triggers an event, it bubbles up to the parent
  3. Use event.target to identify which child element was actually clicked

Benefits:

  • Performance — One listener instead of hundreds (important for large lists)
  • Dynamic elements — Works automatically for elements added later via JavaScript
  • Less memory — Fewer event listeners means lower memory usage
  • Cleaner code — Single handler instead of attaching/removing listeners

When to use:

  • Lists with many clickable items
  • Dynamically generated content
  • Tables, grids, or any repeating UI pattern

Code Examples

Event delegation on a listJavaScript
// ❌ Without delegation — listener on every item
const items = document.querySelectorAll('li');
items.forEach(item => {
  item.addEventListener('click', (e) => {
    console.log(e.target.textContent);
  });
});

// ✅ With delegation — one listener on parent
const list = document.querySelector('ul');
list.addEventListener('click', (e) => {
  if (e.target.tagName === 'LI') {
    console.log(e.target.textContent);
  }
});

// New items added later automatically work!
const newItem = document.createElement('li');
newItem.textContent = 'New Item';
list.appendChild(newItem); // clicking this will trigger the handler

Real-World Applications

Use Cases

Dynamic Lists

Managing click handlers on todo lists, comment threads, or feeds where items are frequently added and removed

Infinite Scroll

Using a single scroll handler on a container element for dynamically loaded content

Dynamic Form Fields

Handling events on form inputs that are added or removed dynamically without re-attaching listeners

Mini Projects

Delegated Todo List

beginner

Build a todo app using a single event listener on the parent <ul> to handle all item interactions

Interactive Data Table

intermediate

Build a sortable and filterable table using event delegation for column headers and row actions

Industry Examples

React

Implements event delegation internally by attaching a single listener to the root container and dispatching SyntheticEvent wrappers to component handlers

jQuery

The .on() method supports event delegation with a child selector parameter, handling events for current and future descendant elements

Resources

MDN - Event delegation

docs

JavaScript.info - Event delegation

article

Related Questions

How do you select and manipulate DOM elements in JavaScript?

junior
dom
Previous
How does JavaScript hoisting work?
Next
How does type coercion work in JavaScript?
PrevNext