Undo click of jQuery parent element

Asked

Viewed 214 times

6

As I can prevent the click of the #b it runs the click of the #link, I would like it to alert only "b"

$("#link").on('click',function(){ alert('link') });

$("#b").on('click',function(){ alert('b') });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a id="link">A<b id="b">b</b></a>

  • Related: http://answall.com/a/42112/129

1 answer

5


Just insert a stopPropagation(); after the alert('b'):

$("#link").on('click',function(event){
  alert('link');
  
});

$("#b").on('click',function(event){
  alert('b');
  event.stopPropagation();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a id="link">A<b id="b">b</b></a>

  • Valeeeeu!! Simple command that saves many lives rsrs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.