Facebook Pixel

Event Bubbling and Capturing Explained

The two propagation phases and how to use them.

Event Bubbling and Capturing Explained

Bubbling (Bottom-Up)

Events propagate from the target up to the document:

// clicking button logs: button, div, body, document

Default listeners run in this phase.

Capturing (Top-Down)

Events propagate from the document down to the target:

element.addEventListener("click", handler, true); // capturing phase

Useful for intercepting events before they reach the target.

Combined Example

<form id="form"> <div id="div"> <button id="button">Click</button> </div> </form>
form.addEventListener("click", () => console.log("form capturing"), true); form.addEventListener("click", () => console.log("form bubbling")); div.addEventListener("click", () => console.log("div capturing"), true); div.addEventListener("click", () => console.log("div bubbling")); button.addEventListener("click", () => console.log("button"));

Clicking the button logs: form capturing, div capturing, button, div bubbling, form bubbling.

The Takeaway

Bubbling: bottom-up (target to document), default. Capturing: top-down (document to target), use third arg true. Order: capturing (outer to inner), target, bubbling (inner to outer).

Bubbling propagates from the target up to the document (bottom-up). Capturing propagates from the document down to the target (top-down). Default listeners run in the bubbling phase.

Capturing handlers fire first (outer to inner), then the target's handlers, then bubbling handlers (inner to outer). For example: form capturing, div capturing, button, div bubbling, form bubbling.

Pass true as the third argument to addEventListener: element.addEventListener('click', handler, true). This listens in the capturing (top-down) phase instead of the default bubbling (bottom-up) phase.

Bubbling (bottom-up). The third argument to addEventListener defaults to false, which means the bubbling phase. Pass true for the capturing phase.

To intercept events before they reach the target. For example, logging all clicks before any handler processes them, or implementing a global event interceptor. Most use cases use the default bubbling phase.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.