# How to stop propagation of an event in JavaScript

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Aug 26, 2020, updated Sep 23, 2021. Topics: JavaScript.
Canonical URL: https://attacomsian.com/blog/javascript-dom-stop-propagation-of-events

> Learn how to stop the propagation of an event in both capturing and bubbling phases using the Event.stopPropagation() method in JavaScript.

To prevent an event from further propagation in the capturing and bubbling phases, you can call the `Event.stopPropation()` method in the event handler.

Propagation of an event means bubbling up to parent elements or capturing down to child elements.

```javascript
Event.stopPropation();
```

Let us say you have got the following HTML code snippet:

```html
<div id="wrapper">
    <button id="signup">Sign Up</button>
</div>
```

When you click on the button, the event is bubbled up to the `<div>` element. The following code will display two alerts when you click the button:

```javascript
const div = document.querySelector('#wrapper');
const btn = document.querySelector('#signup');

// add <div> event handler
div.addEventListener('click', (e) => {
    alert('The wrapper box was clicked!');
});

// add button event handler
btn.addEventListener('click', (e) => {
    alert('The button was clicked!');
});
```

To stop the click event from propagating to the `<div>` element, you have to call the `stopPropagation()` method in the event handler of the button:

```javascript
btn.addEventListener('click', (e) => {
    e.stopPropagation();
    
    alert('The button was clicked!');
});
```

> Note that the `Event.stopPropagation()` method does not prevent any default behaviors of the element from occurring; for instance, clicks on links and checkboxes checked are still processed. To [stop default behaviors](https://attacomsian.com/blog/javascript-dom-prevent-default-action-of-an-event), you should use the  `Event.preventDefault()` method.

The `Event.stopPropagation()` method works in all modern browsers and IE9 and above.
