CoreUI ONE — 250+ PRO components and 25+ admin templates for React, Angular, Vue, Bootstrap and Next.js, plus Data Grid, Scheduler, 4,300+ PRO icons and the Next.js boilerplate. One license. → See what you get

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 set focus on an input field after rendering in React
How to set focus on an input field after rendering in React

Passing props to child components in React function components
Passing props to child components in React function components

How to loop inside React JSX
How to loop inside React JSX

How to validate an email address in JavaScript
How to validate an email address in JavaScript