Christian Benincasa
Christian Benincasa

Reputation: 1215

Trigger event on parent click but not on children

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

Upvotes: 7

Views: 5691

Answers (4)

Deepak Verma
Deepak Verma

Reputation: 1

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/

Upvotes: 0

Jessica Aline
Jessica Aline

Reputation: 1

Change to:

$('.child a').click(function(e) {
    $(this).parent('.child').hide();
});​

Upvotes: 0

VisioN
VisioN

Reputation: 145368

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

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

Upvotes: 14

xCander
xCander

Reputation: 1338

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

Upvotes: 0

Related Questions