7

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?

Relevant jsFiddle

Updated fiddle with text input example

Christian Benincasa
  • 1,215
  • 1
  • 21
  • 45

4 Answers4

14
$(".parent").click(function(e) {
    if (e.target == this) {
        $(this).hide();
    }
});​

DEMO: http://jsfiddle.net/Bt5HA/4/

VisioN
  • 143,310
  • 32
  • 282
  • 281
  • Minor detail, but why not use strict equality? – m59 Mar 09 '15 at 04:28
  • @m59 There is no difference when you compare references, otherwise possible nano difference in speed is not important in this context. – VisioN Mar 09 '15 at 08:20
0

Change to:

$('.child a').click(function(e) {
    $(this).parent('.child').hide();
});​
VisioN
  • 143,310
  • 32
  • 282
  • 281
0

Access child elements and return false when they're clicked http://jsfiddle.net/Bt5HA/3/

xCander
  • 1,338
  • 8
  • 16
0

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/