Skip to the page

The DOM and events · core

Event flow: capture, target, bubble

An event travels down from window to the target (capture), fires on the target, then bubbles back up; stopPropagation stops the travel, stopImmediatePropagation also stops other listeners on the same element, and preventDefault only cancels the browser's default action.

The notes

What you must know

  • addEventListener(type, fn, { capture: true }) listens during the capture phase; default is bubble.
  • event.target is where the event started; event.currentTarget is the element whose listener is running now.
  • stopPropagation: no more elements on the path. stopImmediatePropagation: also no more listeners on this element.
  • preventDefault cancels the default action (link navigation, form submit, checkbox toggle) but does not stop propagation.
  • Some events don't bubble (focus, blur, mouseenter); use focusin/focusout for delegation.

The red pen

Common traps: mistake → fix

  • Mistake: "preventDefault stops bubbling"

    Fix: it only cancels the default action.

  • Mistake: "target and currentTarget are the same"

    Fix: they differ whenever the listener is on an ancestor.

Easy to hard

Questions interviewers ask, easy to hard

  1. Explain capture and bubble with the order of logs for nested elements.
  2. stopPropagation vs stopImmediatePropagation vs preventDefault?
  3. Which events don't bubble?

Practise

Practise with these questions

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview