ReviseAlgo Logo

DOM Manipulation

Event Object & Event Flow

Master event propagation in JavaScript. Learn the three event flow phases: capturing, target, and bubbling, and how to control propagation.

Last Updated: July 15, 2026 10 min read

1. Introduction

When an event is triggered on a DOM element, it doesn't just execute on that element. It travels through the DOM tree in a specific sequence known as Event Flow (or Event Propagation), which consists of three phases: Capturing, Target, and Bubbling.

2. Why It Matters

Failing to manage event propagation can cause unintended behavior, such as parent container click handlers triggering unexpectedly when you click a child button. Understanding event flow allows you to control propagation using methods like stopPropagation().

3. Real-World Analogy

Think of a Corporate Communication Chain:

  • Capturing Phase (Top-Down Memo): An announcement sent from corporate headquarters down the chain. The memo travels from the CEO (root node) through managers (parent nodes) until it reaches the target employee.
  • Target Phase (Action at Target): The memo reaches the target employee (target node), who reads and processes the instruction.
  • Bubbling Phase (Bottom-Up Report): The confirmation report travels back up the chain, from the employee through managers all the way back to the CEO. Each manager can inspect the report as it passes by.

4. The Propagation Phases

An event travels in a U-shaped path through the DOM:
1. Capturing Phase: The event travels down from the window and document root nodes to the target element's parent. By default, listeners ignore this phase unless the capture option is set to true.
2. Target Phase: The event fires on the target element.
3. Bubbling Phase: The event bubbles back up from the target element's parent to the root nodes. By default, listeners register for this phase.

Controlling Propagation:

  • event.stopPropagation(): Prevents the event from propagating further up or down the DOM tree.
  • event.stopImmediatePropagation(): Prevents the event from propagating and stops other listeners registered on the same element from running.
  • event.preventDefault(): Cancels the browser's default action associated with the event (e.g. following link URLs or submitting forms).

5. Target vs CurrentTarget

The event object has two properties that identify elements:
event.target: The element that triggered the event (the origin node where the click happened).
event.currentTarget: The element currently handling the event (the node where the event listener is attached).

6. Practical Example

This script demonstrates how target and currentTarget behave inside nested containers:

7. Common Mistakes

  • Overusing stopPropagation: Preventative calling of stopPropagation stops global analytics hooks or modal close triggers from working by blocking events before they reach the document root. Only call it when explicitly necessary.

8. Quick Quiz

Q1: Which property identifies the element that triggered the event, rather than the element handling the listener?

A) event.currentTarget

B) event.target

Answer: B — event.target references the element where the event originated, while event.currentTarget references the element handling the event.

9. Scenario-Based Challenge

The Overlay Modal Dismissal:

You build a modal overlay:

. Clicking the background overlay should dismiss the modal, but clicking inside the modal box should not close it. Write the event handlers using propagation controls.

10. Debugging Exercise

Explain why clicking a menu link causes the entire menu to collapse instantly:

const menu = document.getElementById('sidebar');
const links = document.querySelectorAll('.menu-link');

menu.addEventListener('click', () => { menu.classList.add('collapsed'); // Collapses menu on background click });

links.forEach(link => { link.addEventListener('click', (e) => { // Objective: navigate without collapsing menu console.log('Navigating...'); }); });

View Solution

Diagnosis: Clicking a link triggers the link handler, but the event bubbles up the DOM tree, eventually executing the click listener on the parent menu element, which collapses the menu.

Fix: Stop the event from bubbling up the DOM tree inside the link click handler:

link.addEventListener('click', (e) => {
  e.stopPropagation(); // Stop event bubbling
  console.log('Navigating...');
});

11. Interview Questions

🟢 Q1: Describe the three phases of event propagation in the DOM.

Answer:
1. Capturing Phase: The event travels down from the window and document root nodes to the target element's parent. By default, listeners ignore this phase.
2. Target Phase: The event fires on the target element itself.
3. Bubbling Phase: The event bubbles back up from the target element's parent to the root nodes. By default, listeners register for this phase.

12. Production Considerations

  • Event Delegation Optimization: Rather than attaching separate event listeners to hundreds of child elements, attach a single event listener to a parent container and inspect event.target to handle events. This reduces memory usage and simplifies code.