Undo the parent element jQuery

6

How can I prevent a #b click from executing the #link click, 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><aid="link">A<b id="b">b</b></a>
    
asked by anonymous 17.12.2015 / 14:21

1 answer

5

Just insert a stopPropagation(); after 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><aid="link">A<b id="b">b</b></a>
    
17.12.2015 / 14:27