How to stop event propagation in JavaScript

Stopping event propagation prevents events from bubbling up to parent elements, giving you precise control over which elements respond to events. As the creator of CoreUI, a widely used open-source UI library, I’ve used stopPropagation extensively in nested components like modals, dropdowns, and complex UI interactions. From my expertise, the stopPropagation() method is crucial when you have overlapping interactive elements and need to prevent unwanted event triggers. This approach ensures events are handled exactly where intended without affecting parent elements.

Use event.stopPropagation() to prevent an event from bubbling up to parent elements.

document.querySelector('.child').addEventListener('click', (event) => {
  event.stopPropagation()
  console.log('Only child handler runs')
})

Here event.stopPropagation() stops the click event from bubbling up to parent elements that might also have click handlers. Without this method, clicking the child element would trigger handlers on both the child and any parent elements, which is often undesired behavior in complex UI components.

Best Practice Note:

Use stopPropagation() sparingly, as it can make debugging event flow more difficult. Consider event delegation patterns as an alternative. This is the same approach we use in CoreUI components like modals and dropdowns to prevent clicks from closing overlays unintentionally.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
How to Convert a Map to an Array in JavaScript
How to Convert a Map to an Array in JavaScript

How to Merge Objects in JavaScript
How to Merge Objects in JavaScript

What is the difference between sort and toSorted in JavaScript?
What is the difference between sort and toSorted in JavaScript?

How to convert a string to boolean in JavaScript
How to convert a string to boolean in JavaScript

Answers by CoreUI Core Team