Course contentsShow
← Web Development
Lesson 758 of 2,98213. DOM EventsFree lesson

What is Event Delegation?

Understanding how event delegation works by attaching a single listener to a parent element instead of many children.

What is Event Delegation?

You'll learn how to handle events for multiple elements efficiently by using a single listener on their parent.

The Core Idea

Imagine you're managing a classroom with 30 students. You could give each student a personal walkie-talkie to communicate with you, or you could simply stand near the group and listen to whoever speaks up. Event delegation is like that second approach—instead of attaching event listeners to every single child element, you attach one listener to their common parent and let events bubble up to it.

When an event occurs on a child element, it bubbles up through the DOM tree (remember event bubbling?). The parent's listener catches the event and checks which specific child triggered it using event.target.

Why This Matters

Consider a list with 100 items. Without delegation, you'd attach 100 separate event listeners:

Instead of:

  • Listener on Item 1
  • Listener on Item 2
  • Listener on Item 3
  • ...100 times

With delegation:

  • One listener on the parent <ul> that handles all clicks

The parent listener examines event.target to determine which list item was actually clicked. This approach uses less memory, improves performance, and automatically works for dynamically added items—you don't need to attach new listeners when elements are added later.

When to Use It

Event delegation shines when you have many similar elements (like buttons, list items, or table rows) that need the same type of event handling. The parent catches the bubbled event and acts based on which child triggered it.

Key Takeaway: Event delegation attaches one listener to a parent element that handles events from multiple children, improving performance and automatically supporting dynamically added elements.