Sunday, 25 March 2018

jquery stop child triggering parent event



I have a div which I have attached an onclick event to. in this div there is a tag with a link. When I click the link the onclick event from the div is also triggered. How can i disable this so that if the link is clicked on the div onclick is not fired?



script:



$(document).ready(function(){
$(".header").bind("click", function(){
$(this).children(".children").toggle();
});

})


html code:




some link

  • some list





Answer



Do this:



$(document).ready(function(){
$(".header").click(function(){
$(this).children(".children").toggle();
});
$(".header a").click(function(e) {

e.stopPropagation();
});
});


If you want to read more on .stopPropagation(), look here.


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...