What is the use of stopPropagation method in JavaScript

The stopPropagation() method prevents events from bubbling up through the DOM tree to parent elements. When an event occurs on a child element, it normally triggers on all parent elements too - this method stops that behavior.

The Event Bubbling Problem

In JavaScript, events "bubble up" from child to parent elements. For example, if a button is inside a div and both have click handlers, clicking the button triggers both events. The stopPropagation() method prevents this propagation.

Syntax

event.stopPropagation();

Example: Event Bubbling Without stopPropagation

<!DOCTYPE html>
<html>
<head>
   <title>Event Bubbling Demo</title>
   <style>
      .outer {
         padding: 50px;
         background-color: lightblue;
         cursor: pointer;
      }
      .inner {
         padding: 20px;
         background-color: lightcoral;
         cursor: pointer;
      }
   </style>
</head>
<body>
   <div class="outer" onclick="outerClick()">
      Outer Div
      <div class="inner" onclick="innerClick(event)">
         Inner Div (Click me)
      </div>
   </div>
   
   <p>Check to stop propagation: <input type="checkbox" id="stopProp"></p>
   
   <script>
      function innerClick(event) {
         alert("Inner div clicked!");
         if (document.getElementById("stopProp").checked) {
            event.stopPropagation();
         }
      }
      
      function outerClick() {
         alert("Outer div clicked!");
      }
   </script>
</body>
</html>

Example: Button Inside Div Container

<!DOCTYPE html>
<html>
<head>
   <title>Button stopPropagation Example</title>
   <style>
      .container {
         padding: 40px;
         background-color: #f0f0f0;
         text-align: center;
         cursor: pointer;
      }
      button {
         padding: 10px 20px;
         font-size: 16px;
         cursor: pointer;
      }
   </style>
</head>
<body>
   <div class="container" onclick="divClicked()">
      Container Div
      <br><br>
      <button onclick="buttonClicked(event)">Click Button</button>
   </div>
   
   <script>
      function buttonClicked(event) {
         event.stopPropagation();
         alert("Button clicked - propagation stopped!");
      }
      
      function divClicked() {
         alert("Container div clicked!");
      }
   </script>
</body>
</html>

Difference: stopPropagation vs preventDefault

Method Purpose Use Case
stopPropagation() Stops event bubbling to parent elements Prevent parent handlers from triggering
preventDefault() Prevents browser's default behavior Stop form submission, link navigation, etc.

Example: preventDefault Method

<!DOCTYPE html>
<html>
<head>
   <title>preventDefault Example</title>
</head>
<body>
   <a href="/javascript/index.htm" onclick="linkClicked(event)">
      Visit JavaScript Tutorial
   </a>
   
   <script>
      function linkClicked(event) {
         event.preventDefault();
         alert("Link click prevented - navigation stopped!");
      }
   </script>
</body>
</html>

When to Use stopPropagation

Use stopPropagation() when you want to handle an event on a specific element without triggering handlers on parent elements. Common scenarios include:

  • Buttons inside clickable containers
  • Form controls within clickable rows
  • Nested interactive elements

Conclusion

The stopPropagation() method is essential for controlling event flow in nested DOM elements. Use it to prevent unwanted parent event handlers from executing when child elements are clicked.

Updated on: 2026-03-15T23:19:00+05:30

686 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements