Special Offer Diwali Special Bundle Offer: 3 courses for ₹8,999, save ₹9,001 (50% OFF)! Limited seats available. View Bundles →
0% Completed
Tutorials / JavaScript Tutorial / Chapter 4: DOM Manipulation & Events
JAVASCRIPT ⏱ 10 min read

Event Bubbling, Capturing, and Delegation

Instead of attaching 100 individual event listeners to 100 list items, event delegation lets you attach a single listener to their parent container.

What is it?

Event Bubbling is the process where an event triggered on a child element propagates ('bubbles') upward through its ancestors in the DOM tree. Event Delegation is a technique that leverages bubbling to handle events at a parent level.

Why is it used?

Improves memory performance and automatically handles dynamically created elements added to the DOM later.

Detailed Explanation

Event Phases: 1. Capturing Phase: The event travels down from `window` to the target element. 2. Target Phase: The event reaches the target element. 3. Bubbling Phase: The event bubbles back up from the target to `window`. Event Delegation Pattern: Attach ONE listener to the parent container. Inside the callback, use `e.target.closest('.item-btn')` to check if a relevant child element was clicked.

Syntax Structure

Syntax Blueprint
parentContainer.addEventListener('click', (e) => {
  const btn = e.target.closest('.delete-btn');
  if (btn) {
    // Handle button action
  }
});

Code Example

JAVASCRIPT Code
const chapterList = document.getElementById("chapterAccordionList");

// Single Event Listener on parent container handles all chapters
chapterList.addEventListener("click", (e) => {
  // Find clicked chapter header toggle button
  const headerBtn = e.target.closest(".chapter-header-btn");
  if (!headerBtn) return; // Ignore clicks elsewhere inside container

  const chapterContent = headerBtn.nextElementSibling;
  const isExpanded = headerBtn.getAttribute("aria-expanded") === "true";

  // Toggle accordion state
  headerBtn.setAttribute("aria-expanded", !isExpanded);
  chapterContent.classList.toggle("collapsed");
});

Explanation of the Code

• One listener on `chapterList` handles clicks across all chapters. • `e.target.closest()` matches the header button even if user clicks an internal icon or child text node.

Expected Output

Smooth chapter accordion collapsing and expanding with minimal memory footprint.

Real-World Application

Used across sidebar menus, data tables, and kanban boards with hundreds of interactive rows.

Common Mistakes to Avoid

• Attaching listeners in a loop inside dynamic render functions, creating memory leaks. • Forgetting `e.stopPropagation()` if you truly want to stop an event from reaching outer listeners.

Quick Practice Challenge

Create an event delegation listener on a `<ul>` that logs the text of whichever `<li>` was clicked.

Key Takeaways

• Events bubble up from the target element through all parent containers. • Event Delegation attaches one listener to a parent to manage many children. • `e.target.closest()` ensures clicks on nested icons still trigger the button.