Javascript code:
$(".div1").click(function(event){
event.stopPropagation();
$(".div1 div").slideToggle();
});
$(".div2").click(function(event){
event.stopPropagation ();
$(".div2 div").slideToggle();
});
$(document).click(function(event){
$(".div1 div,.div2 div").slideUp();
});
Suppose I click .div1 & .div1 div slides down. Then i click .div2 But as event.stopPropagation is executed, .div1 div doesnot slide up. So what should i do? I dont want to put this code $(".div1 div").slideUp(); as it makes no sense when there’ll be too many divs like this
Never use
Event.stopPropagation(). All the layers of any application should always, at any point, be able to register an event happened.Instead use the
event.targetand traverse with.closest()to achieve the same.The following will handle infinite numbers of DIVs, so you don’t need to rewrite your code. (PS: just add a
.boxto your parent elements to make all simpler)