If I have a parent div that is positioned absolutely and then a child div that has a higher z-index and is positioned relatively, is there a way to have a click event register only if the parent div is clicked, but not the inside div?
4 Answers
$(".parent").click(function(e) {
if (e.target == this) {
$(this).hide();
}
});
Access child elements and return false when they're clicked http://jsfiddle.net/Bt5HA/3/
1 Comment
xCander
VisioN has the best solution, ignore this post
Change to:
$('.child a').click(function(e) {
$(this).parent('.child').hide();
});
1 Comment
j08691
That only works if the child is clicked. What happens if the parent is clicked?
Try This
$('#child').click(function(event) {
event.stopPropagation();
alert('You clicked Child');
});
$('#parent').click(function() {
alert('You clicked on Parent');
});
You can check working here http://jsfiddle.net/VnHGh/24/