Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2023-09-11 12:05:08
forward
721 people have browsed it

如何使用 JavaScript 中的内联 onclick 属性停止事件传播?

Sometimes, we need to add the same event on nested HTML elements. For example, we have two divs, one is the parent div and the other is the child div. Now, we need to add onclick events on parent and child divs and perform different functions when user clicks on parent and child divs. In this case it will always execute the event on the parent and child divs.

Let us see how to perform the same event on a nested HTML element with the following example.

Example

In the example below, we create two divs. We gave the outer div the "parent-div" class name and the inner div the "child-div" class name.

In addition, we also added an onclick event to perform different functions on the two div elements. When the user clicks on the inner div, the click event is also fired in the parent div.

<html>
<head>
   <style>
      .parent-div {
         width: 300px;
         height: 150px;
         margin: 50px;
         padding: 10px;
         background-color: lightblue;
      }
      .child-div {
         width: 100px;
         height: 70px;
         margin: 30px; 
         padding: 10px;
         background-color: lightgreen;
      }
   </style>
</head>
<body>
   <h3>Adding the same events <i> to nested HTML elements </i> in JavaScript</h3>
   <p>Click on the below parent & child DIVs to see the result</p>
   <div class = "parent-div" onclick = "executeParent()"> Parent DIV
      <div class = "child-div" onclick = "executeChild()"> Child DIV </div>
   </div>
   <div id="content"> </div>
   <script>
      let content = document.getElementById("content");
      function executeParent() {
         content.innerHTML += "Parent div clicked <br>";
      }
      function executeChild() {
         content.innerHTML += "Child div clicked <br>";
      }
   </script>
</body>
</html>
Copy after login

We need to use events to solve the above problems, such as clicking on the child div and calling the click event.stopPropogation() method of the parent div.

grammar

Users can use the stopPropgation() method according to the following syntax to stop propagating events to the parent element.

Event.stopPropogation(); 
Copy after login

Example

In the example below, we have added some text inside the HTML

tag and added some text in the onclick event that executes the executeP() function. Additionally, we added a

tag inside the tag and added an "onclick" event on the span tag that executes the executeSpan() function.

Additionally, we used the event.stoPropogation() method with the onclick event of the element to stop propagating events on the tag when the user clicks on the

.

In the output, the user can observe that when they click on the text of the element, it only executes the executeSpan() function.

<html>
<body>
   <h3>Adding the same events <i> to nested HTML elements and using the event.stopPropogation() method </i> in JavaScript </h3> 
   <p style = "cursor: pointer;" onclick="executeP()"> Hello users! How are you? <span Onclick = "event.stopPropagation(); executeSpan();"> Child Span </span> </p>
   <div id = "content"> </div>
   <script>
      let content = document.getElementById("content");
      function executeP() {
         content.innerHTML += "Clicked on the p element. <br>";
      }
      function executeSpan() {
         content.innerHTML += "Clicked on the Span element! <br>";
      }
   </script>
</body>
</html> 
Copy after login

Example

In the example below, we create a hierarchy of three nested elements using HTML markup. We added an onclick event on each element. If we don't use the event.stopPropogation() method, it always executes the firstDivClick() function.

To solve this problem, we pass the events as parameters of the function when calling them and use the stopPropogation() method inside the function.

Users can observe that when they click on the Menu text, it only executes the kebabMenuClick() function. When the user clicks on the second div, it simply executes the secondaryDivClick() function.

<html>
<head>
   <style>
      .card-1 {
         width: 300px;
         height: 200px;
         background-color: red;
         cursor: pointer;
      }
      .card-2 {
         width: 200px;
         height: 150px;
         background-color: blue;
         cursor: pointer;
      }
      .kebab-menu {
         font-size: 1.2rem;
         color: white;
         cursor: pointer;
      }
   </style>
</head>
<body>
   <h3>Adding the same events <i> to nested HTML elements and using the event.stopPropogation() method </i> in JavaScript </h3>
   <div class = "card-1" onclick = "firstDivClick(event)">
      <div class = "card-2" onclick = "secondDivClick(event)">
         <div class = "kebab-menu" onclick = "kebabMenuClick(event)"> Menu </div>
      </div>
   </div>
   <div id = "content"> </div>
   <script>
      let content = document.getElementById("content");
      function firstDivClick(event) {
         content.innerHTML += "first div clicked <br>";
      }
      function secondDivClick(event) {
         event.stopPropagation();
         content.innerHTML += "second div clicked <br>";
      }
      function kebabMenuClick(event) {
         event.stopPropagation();
         content.innerHTML += "kebab menu clicked <br>";
      }
   </script>
</body>
</html>
Copy after login

Users learned to use the event.stopPorpogation() method on events. Basically, it is used to prevent events from propagating to the parent element. This way, when the same event triggers a parent-child element, we can execute different functions for all child elements.

The above is the detailed content of How to stop event propagation using inline onclick attribute in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!