How can I prevent clicking on the outer element while clicking on the inner element with js events?
It is quite simple, let’s see the below example code.
<div id=”parent_element” >So, when we click on the inner element, it will actually trigger the two events one is the inner element and another will be of the parent element.
This is also called event bubbling in javascript. Where Bubbling means the most of the javascript events trigger from target element to its parent elements.
Event bubbling start from bottom to up elements.
Javascript example to use event stopPropagation
Javascript code is as given below.
document.querySelector("#parent_element").addEventListener('click', function() { alert('outer element'); }) document.querySelector("#child_element").addEventListener('click', function(e) { e.stopPropagation(); alert('inner element'); })In the jQuery use of event stop propagation
In jQuery the solution will be like below.
$(“#parent_element”).on(‘click’, function() { alert(‘outer element’); }) $(“#child_element”).on(‘click’, function(e) { e.stopPropagation(); alert(inner element’); })Hence, we can use stop propagation on the inner element to prevent its click bubbling to the outer element.
Today we are going to learn about managing multiple PHP versions on ubuntu with xampp.…
Let's understand about how to use coding to improve your website's SEO. In today’s computerized…
Let's understand the most important linux commands for web developers. Linux, as an open-source and…
Today we are going to discuss top 75+ Laravel interview questions asked by top MNCs.Laravel,…
Today we will discuss about the Mailtrap integration with laravel 10 .Sending and receiving emails…
Today we are going to integrate FCM (Firebase Cloud Messaging) push notifications with ionic application.Firebase…