Event Capturing
Capturing is the phase where an event travels from window down the ancestor chain to the target, before the target's own listeners and before bubbling. You opt in with { capture: true }.
Detailed Explanation
The full path of a bubbling event is: capture (window → target), target phase, bubble (target → window). By default addEventListener registers for bubble (and target). Passing true as the third argument or { capture: true } registers for capture (and target).
Capture listeners on a parent run before the child's listeners. That is how you intercept: a document-level capture handler can see a click before a widget's own stopPropagation in the bubble phase — but the child can still stopPropagation during capture if it also has a capture listener.
removeEventListener must pass the same capture flag. A bubble listener and a capture listener are two registrations. Removing one does not remove the other.
Most application code never needs capture. Delegation works on the bubble phase. Capture shows up for: "close the menu on first interaction, before the click opens something else", some focus management, and libraries that want to hear events even when a child stops bubble.
event.eventPhase is CAPTURING_PHASE on the way down. event.target is still the origin.
What it costs you: two listeners that look identical and fire twice if you registered both phases by mistake. Also, capture + stopPropagation is even harder to debug than bubble stop, because the child's own bubble code never runs.
Reach for capture when you must run before the target. Reach for bubble for normal parent handlers. Do not register capture "just in case".
A modal that must close on outside click often uses capture on document so it runs before a button inside the page starts a drag or a focus move. The handler checks event.target against the dialog node. If you bind this in bubble instead, a child that stops propagation (a map widget, a third-party date picker) will keep the modal open.
Do not mix capture close and bubble open on the same click without event.timeStamp or a flag; the same click that opens can also close.
Code Examples
const outer = document.querySelector("#outer");
const inner = document.querySelector("#inner");
outer.addEventListener(
"click",
() => console.log("outer capture"),
{ capture: true },
);
outer.addEventListener("click", () => console.log("outer bubble"));
inner.addEventListener("click", () => console.log("inner"));
// click inner:
// outer capture → inner → outer bubbleconst onClick = () => {};
el.addEventListener("click", onClick, true);
el.removeEventListener("click", onClick, true); // flag must match
el.removeEventListener("click", onClick); // does not remove the capture oneCommon Gotchas
Remove must match capture
`removeEventListener(type, fn)` leaves a `{ capture: true }` listener in place.
Capture `stopPropagation` prevents the target's bubble listeners
the child may look "dead".