Friday, September 5, 2025
HomeLanguagesJavascriptHow to stop event propagation with inline onclick attribute in JavaScript ?

How to stop event propagation with inline onclick attribute in JavaScript ?

Use HTML DOM stopPropagation() method to stop the event from propagating with inline onclick attribute which is described below:

HTML DOM stopPropagation() Event Method: The stopPropagation() method is used to stop propagation of event calling i.e. the parent event is called we can stop the propagation of calling its children by using the stopPropagation() method and vice-versa.

Syntax:

event.stopPropagation()

Example 1: This example stops the event propagation by adding stopPropagation method on onclick the <span> element.




<!DOCTYPE HTML> 
<html> 
    <head> 
        <title> 
            How to stop event propagation with
            inline onclick attribute
        </title>
          
        <style>
            div {
                background: green;
            }
            span {
                background: red;
                color: white;
            }
        </style>
    </head> 
      
    <body style = "text-align:center;"> 
      
        <h1 style = "color:green;" > 
            GeeksForGeeks 
        </h1> 
          
        <div onclick= "alert('you clicked the div')">
            <span onclick= "event.stopPropagation(); 
            alert('you clicked Span element(inside the div)');">
                Span Content
            </span>
        </div>
    </body> 
</html>                    


Output:

  • Before clicking on the element:
  • After clicking on the <span> element:

Example 2: This example stops the event propagation by adding stopPropagation() method on onclick to the <span> element. This example handles the Internet Explorer case by setting window.event.cancelBubble to true.




<!DOCTYPE HTML> 
<html> 
    <head> 
        <title> 
            How to stop event propagation with
            inline onclick attribute
        </title>
          
        <style>
            div {
                background: green;
            }
            span {
                background: red;
                color: white;
            }
        </style>
    </head> 
      
    <body style = "text-align:center;"> 
      
        <h1 style = "color:green;" > 
            GeeksForGeeks 
        </h1> 
          
        <div onclick= "alert('you clicked the div')">
            <span onclick= "StopEventPropagation(event);
            alert('you clicked Span element(inside the div)');">
                Span Content
            </span>
        </div>
          
        <br>
        <script>
            function StopEventPropagation(event) {
                if (event.stopPropagation) {
                    event.stopPropagation();
                }
                else if(window.event) {
                    window.event.cancelBubble=true;
                }
            }     
        </script> 
    </body> 
</html>                    


Output:

  • Before clicking on the element:
  • After clicking on the <span> element:
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6637 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11865 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7027 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS