


How to stop event propagation using inline onclick attribute in JavaScript?
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>
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();
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. 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. 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!<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>
Example
<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>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Data update problems in zustand asynchronous operations. When using the zustand state management library, you often encounter the problem of data updates that cause asynchronous operations to be untimely. �...
