DEV Community

sakethk
sakethk

Posted on

Mastering JS Event Bubbling and Delegation

As a web developer, you might have encountered scenarios where you need to handle events on a large number of elements. Handling events can be resource-intensive, especially if you add an event listener to each element individually. JS event bubbling and event delegation are two approaches that can help you manage events more efficiently. In this article, we'll explore what JS event bubbling and event delegation are and how they can be used to handle events.

JS Event Bubbling:

When an event occurs on an element in the DOM tree, it starts from that element and then propagates upwards to its ancestors. This process is called event bubbling. Let's take an example to understand this better. Consider a simple HTML structure:

<div id="parent">
  <div id="child"></div>
</div>
Enter fullscreen mode Exit fullscreen mode

If we add a click event listener to the child element, and then click on the child, the event will first trigger on the child element and then propagate up to its parent element. In other words, the event bubbles up from the child to its parent.

const child = document.querySelector("#child");
child.addEventListener("click", () => {
  console.log("Child clicked");
});
Enter fullscreen mode Exit fullscreen mode
const parent = document.querySelector("#parent");
parent.addEventListener("click", () => {
  console.log("Parent clicked");
});
Enter fullscreen mode Exit fullscreen mode

In this example, when we click on the child element, the event listener attached to the child element will execute first, and then the event listener attached to the parent element will execute. This is because the event bubbles up from the child to its parent.

JS Event Delegation:

JS event delegation is a technique that leverages event bubbling to handle events more efficiently. Instead of adding an event listener to each element individually, we can add a single event listener to a parent element and then use event.target to determine which child element was clicked.

const parent = document.querySelector("#parent");
parent.addEventListener("click", (event) => {
  console.log(event.target);
});
Enter fullscreen mode Exit fullscreen mode

In this example, we have added a click event listener to the parent element. When we click on the child element, the event bubbles up to the parent element, and the event listener attached to the parent element executes. The event object passed to the event listener has a target property that refers to the element on which the event occurred. We can use this property to determine which child element was clicked.

Benefits of Event Delegation:

Using event delegation has several benefits:

Less code: With event delegation, we can add a single event listener to a parent element instead of adding event listeners to each child element individually. This can help reduce the amount of code we need to write.

Improved performance: Handling events on a large number of elements can be resource-intensive. By using event delegation, we can reduce the number of event listeners we need to add, which can improve performance.

Dynamic elements: If we add new elements to the DOM dynamically, we don't need to add event listeners to these elements individually. Instead, we can add a single event listener to a parent element that handles events for all child elements, even the ones added dynamically.

Conclusion:

In conclusion, JS event bubbling and event delegation are two techniques that can help you manage events more efficiently. Event bubbling allows events to propagate upwards from the element on which they occurred to its ancestors. Event delegation allows you to add a single event listener to a parent element and handle events for all child elements. By using event delegation, you can write less code, improve performance, and handle events for dynamic elements more efficiently. I hope this article has given you a better understanding of these techniques and how to use them to handle events in your projects.

Top comments (0)