Event Bubbling And Event Capturing in JavaScript DOM

Event Bubbling And Event Capturing in JavaScript DOM

Greetings to all the JavaScript enthusiasts learning on codedamn! Today, we're going to delve into a fundamental aspect of JavaScript's Document Object Model (DOM) – Event Bubbling and Event Capturing. We'll explore what they are, how they function, and how to manipulate them to our advantage in JavaScript. This blog post is intended for developers who have a basic understanding of JavaScript and the DOM but want to deepen their knowledge on how events are handled in JavaScript.

Understanding DOM Events

Before we jump into Event Bubbling and Event Capturing, we need to have a basic understanding of what DOM events are. Essentially, a DOM event is a signal that something has happened. This can be a user clicking on a button, a page finishing loading, or even an error occurring. JavaScript allows us to listen to these events and define functions, known as event handlers, to be executed when the event occurs.

let button = document.querySelector('button'); button.addEventListener('click', function() { console.log('Button clicked!'); });

In the above example, we're using the addEventListener method to listen for a 'click' event on a button. When the button is clicked, our event handler function is executed, and 'Button clicked!' is logged to the console.

What is Event Bubbling?

Event bubbling is a type of event propagation in the HTML DOM API when an event occurs in an element inside another element, and both elements have registered a handle for that event. With bubbling, the event is first captured and handled by the innermost element and then propagated to outer elements.

Consider the following HTML structure:

<div id="parent"> <button id="child">Click me!</button> </div>

And the following JavaScript:

let parent = document.querySelector('#parent'); let child = document.querySelector('#child'); parent.addEventListener('click', function() { console.log('Parent clicked!'); }); child.addEventListener('click', function() { console.log('Child clicked!'); });

If we click on the button, we'll see both 'Child clicked!' and 'Parent clicked!' logged to the console. This is because the click event starts at the button, and then bubbles up to the parent div.

What is Event Capturing?

Event capturing is the opposite of event bubbling. Instead of starting from the most specific node and event and then moving outwards, it does the exact opposite. The event is first captured by the outermost element and propagated to the inner elements.

We can use the third parameter of the addEventListener method to change the propagation mode to capturing. By default, it's false (bubbling), but we can set it to true to switch to capturing.

parent.addEventListener('click', function() { console.log('Parent clicked!'); }, true); child.addEventListener('click', function() { console.log('Child clicked!'); }, true);

Now, if we click on the button, we'll see 'Parent clicked!' logged to the console before 'Child clicked!'. This is because the click event starts at the parent div, and then is captured down to the button.

Event.stopPropagation()

The event.stopPropagation() method is used to stop the propagation of an event, either in the bubbling phase or capturing phase. This means that if you click on the child element, the parent won't be notified of this event.

child.addEventListener('click', function(event) { console.log('Child clicked!'); event.stopPropagation(); });

FAQ

Q: Can I use both event bubbling and capturing at the same time?

A: No, an event can either be handled in the capturing phase or the bubbling phase, but not both.

Q: Can I stop event propagation in the capturing phase?

A: Yes, event.stopPropagation() works in the capturing phase as well as the bubbling phase.

Q: What's the difference between event.stopPropagation() and event.stopImmediatePropagation()?

A: event.stopImmediatePropagation() not only stops the event from propagating to parent elements, but also prevents other listeners on the same element from being executed.

For further reading and understanding, you can check out the official documentation on EventTarget.addEventListener().

I hope this blog post helped you gain a better understanding of event bubbling and capturing in JavaScript's DOM. Remember, practice is key to mastering these concepts. Happy coding on codedamn!

Sharing is caring

Did you like what Sarthak Jain wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far