DEV Community

DIWAKARKASHYAP
DIWAKARKASHYAP

Posted on

2 1 1 1 1

What is Event Bubbling? event bubbling in JS

Event bubbling in JavaScript refers to the way events are handled and propagated through the different elements of a webpage. When an event occurs on an element, such as a button click, the event doesn't just trigger on that specific element. Instead, it also triggers on its parent elements, all the way up to the topmost element (usually the document).

Imagine a scenario where you have a button inside a div, and that div is inside another div. If you click the button, the click event will not only be detected by the button itself but also by its parent div and the outer div.

This bubbling behavior allows you to capture events at different levels of the DOM hierarchy. For example, you can have a single event listener attached to the outer div, and it will be triggered for events that occur on the button, the inner div, or the outer div itself.

Event bubbling can be useful because it simplifies event handling. Instead of attaching event listeners to each individual element, you can attach a single listener to a parent element and let the event bubble up to it. This reduces the amount of code you need to write and makes it easier to manage events.

However, in some cases, you may want to stop the event from bubbling further up the DOM hierarchy. You can use the event.stopPropagation() method inside an event handler to prevent the event from triggering on parent elements. This can be handy when you want to isolate the event handling to a specific element and prevent any further handling by its ancestors.

In summary, event bubbling in JavaScript refers to the propagation of events from a child element to its parent elements. It simplifies event handling by allowing you to capture events at higher levels in the DOM hierarchy.

Thank you for reading this blog, follow me on Twitter, I regularly share blogs and post on Javascript, React, Web development and opensource contribution

Twitter- https://twitter.com/Diwakar_766

Github- https://github.com/DIWAKARKASHYAP

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay