jQuery on() stopPropagation not working?
use event.stopImmediatePropagation() It worked for me
Since you are using on
on the body
element and not directly on img.theater
the event is going to bubble up to body
element and that is how it works.
In the course of event bubbling .theater-wrapper
elements click
event will be triggered so you are seeing it.
If you are not creating any dynamic elements then attach the click event handler directly on img.theater
element.
$("img.theater").click(function(event){
event.stopPropagation();
$('.theater-wrapper').show();
});
Alternatively you can check the target of the click
event inside .theater-wrapper
elements click
handler and do nothing.
$(".theater-wrapper").click(function(event){
if ($(event.target).is('img.theater')){
event.stopPropagation();
return;
}
$('.theater-wrapper').hide();
});
The best way to do that is :
$(".theater-wrapper").click(function(event){
if (!$(event.target).is('img.theater')){
$('.theater-wrapper').hide();
}
});
It's working for me with an accordion and checkbox