🟢 EasyDOM & Events📖 Theory Question

What is the difference between event bubbling and event capturing?

💡

Hint

3 phases: capture (down), target, bubble (up) — addEventListener default is bubble

Full Answer

When an event fires, it passes through 3 phases in the DOM tree:

  1. Capture phase — event travels DOWN: window → document → ... → target
  2. Target phase — event is at the element that was clicked/triggered
  3. Bubble phase — event travels UP: target → ... → document → window
// Capture phase listener (3rd arg = true, or { capture: true })
document.body.addEventListener('click', () => console.log('body capture'), true);

// Bubble phase listener (default)
document.body.addEventListener('click', () => console.log('body bubble'));
button.addEventListener('click', () => console.log('button'));

// Click the button:
// body capture (capturing, going down)
// button       (at target)
// body bubble  (bubbling, going up)

// Stop bubbling
button.addEventListener('click', (e) => {
  e.stopPropagation(); // stops bubble — body bubble won't fire
  // e.stopImmediatePropagation() — also blocks same-element listeners
});

Events that don't bubble: focus, blur, scroll, mouseenter, mouseleave — use focusin/focusout for delegation instead.

💡 Event delegation relies on bubbling — one listener on the parent handles events from all children. This is more efficient than attaching listeners to each child.

More DOM & Events Questions

🟢 EasyExplain event delegation and why it is useful.→🟢 EasyWhat is the difference between event.stopPropagation() and event.preventDefault()?→🟢 EasyHow do you create and dispatch Custom Events?→🟡 MediumWhat is MutationObserver and when do you use it?→

Practice this in a timed sprint →

5 free questions, no signup required

âš¡ Start Sprint