FrontendDeveloper.in

JavaScript question detail

What is event capturing

Event capturing is a phase of event propagation in which an event is first intercepted by the outermost ancestor element, then travels downward through the DOM hierarchy until it reaches the target (innermost) element.

To handle events during the capturing phase, you need to pass true as the third argument to the addEventListener method.

<button class="child">Hello</button>

<script>
const parent = document.querySelector("div");
const child = document.querySelector(".child");

// Capturing phase: parent listener (runs first)
parent.addEventListener("click", function () {
console.log("Parent (capturing)");
}, true); // `true` enables capturing

// Bubbling phase: child listener (runs after)
child.addEventListener("click", function () {
console.log("Child (bubbling)");
});
</script>
// Parent (capturing)
// Child (bubbling)
Back to all JavaScript questions
Get LinkedIn Premium at Rs 399