Member-only story

Understanding Event Capturing in JavaScript

Learn how event capturing works and elevate your coding skills with practical examples

Max N
2 min readApr 10, 2024
Photo by Joan Gamell on Unsplash

Have you ever wondered what happens behind the scenes when you click on an element in a webpage? The answer lies in event propagation, specifically event capturing. This mechanism allows developers to control how events are handled in their applications.

Let’s dive in.

What is Event Capturing?

Event capturing is one of two phases of event propagation in JavaScript. When an event occurs, it first goes through the capture phase before reaching its target and then moves on to the bubble phase. During the capture phase, an event starts at the topmost parent node in the DOM tree and travels downward until it reaches the targeted element.

Here’s a visual representation:

<html>
---- event captured here first
<body> ---- event traveled here next
<div> --- final destination
<button></button>
</div>
</body>
</html>

In this example, if you were to attach an event listener to each node and trigger a click event on the button, the order would be as follows:

  1. HTML
  2. BODY

--

--

Max N
Max N

Written by Max N

A writer that writes about JavaScript and Python to beginners. If you find my articles helpful, feel free to follow.

No responses yet