How to ignore click event when clicked on children How to ignore click event when clicked on children jquery jquery

How to ignore click event when clicked on children


You can stop the clicks from bubbling up from links with an additional handler, like this:

$("#block a").click(function(e) { e.stopPropagation(); });

The the alternative we were discussing in comments:

$("#block").delegate('a', 'click', function(e){ e.stopImmediatePropagation(); })           .click(function() { alert('test'); });​

This would prevent any child links from bubbling up (well, not really, but their handlers from executing), but not create a handler for each element, this is done via .stopImmediatePropagation().


This is what you need: http://api.jquery.com/event.target/ .

Just compare to check if the element was triggered by the element you want or one of its children.


You can test which node was clicked with the target property of the event object:

$("#block").click(function(event) {     if(event.target.nodeName != 'A') {        alert('test');    }});

I suggest to read Event Properties from quirksmode.org.