Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 405
Christian Benincasa Avatar asked Jun 20 '12 16:06

Christian Benincasa


People also ask

How do I prevent a parent's onClick event from firing when a child is clicked?

click(function(e) { e. stopPropagation(); }); As said by jQuery Docs: stopPropagation method prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event.


1 Answers

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

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

like image 148
VisioN Avatar answered Sep 22 '22 21:09

VisioN