jQuery | event.stopPropagation() Method

The event.stopPropagation() method is an inbuilt method in jQuery which is used to stop the windows propagation. In the DOM tree when setting an event with the child element and the parent element as well then if you hit on the child element event it will call both child and the parent element as well. So with the help of this method this popup will not appear for the other element except selected element.

Syntax:

event.stopPropagation()

Parameters: It accepts single parameter which is mandatory. This parameter comes from binding function.

Return Value: This method returns the selected element with specified changes made by stopPropagation() method.

Example: This example illustrates the event.stopPropagation() method.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
   
<head>
    <title>
        jQuery event.stopPropagation() Method
    </title>
      
    <script src=
    </script>
      
    <style>
        .main {
            border:1px solid green;
            padding:20px;
            width:60%;
        }
    </style>
      
    <!-- Script to use jQuery event.stopPropagation() Method -->
    <script>
        $(document).ready(function() {
            $(".main").click(function() {
                alert("Main div element");
            });
            $(".GFG").click(function(event) {
                event.stopPropagation();
                alert("Nested div element");
            });
            $(".geeks").click(function(event) {
                alert("Second nested div element");
            });
        });
    </script>
</head>
   
<body>
      
    <!-- Click on element to display alert message -->
    <div class="main">
        GeeksforGeeks
        <div class="GFG">
            A computer science portal
            <div class="geeks">
                Welcome to GeeksforGeeks
            </div>
        </div>
    </div>
   
</body>
   
</html>

chevron_right


Output:

  • Before Clicking the button:
  • After Clicking the button:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.