views:

71

answers:

5

Hi folks,

the noob way to do it I guess would be

$('*').click(function(){...});

But is there any way I can catch any type of click, without having to register a listener for every object in the DOM?


Help would be amazing. =)

+7  A: 

A click event will by default bubble up the DOM, so you can just attach a click handler to the root, like this:

$(document).click(function() {
  //do something
});

Unless a handler on an element along the way does an event.stopPropagation() or return false, you'll get the click here.

Nick Craver
+1, was just writing this. This assumes that no other click handlers cancel the bubbling using `event.stopPropagation()`, of course.
Andy E
Great Thanks Nick!!
RadiantHex
The event.stopPropagation() was very useful to know! Thanks for including this
RadiantHex
@Andy - +1 - Important point, was adding that while you were commenting :)
Nick Craver
+2  A: 

How about:

$('body').click(function(){...});

Any click will be on the body, since that is the parent of all visible nodes in the dom.

pkaeding
+3  A: 

How about just attaching a click to the body?

$('body').click(function(e){ ... })

The e.target should contain what was clicked on

fudgey
+3  A: 

You can use even delegation on the document to catch all clicks.

jQuery will fill the target property of the event to retrieve the clicked element.

$(document).click(function(event){
  // event.target is the clicked object
});

Note that event.target will be the deepest element clicked. Ex: if there is a <span> in a <a>, you will get the <span>, not the <a>.

If you want to catch any click but want to retrieve a specific element (like a class), you can do:

$(document).click(function(event){
  $(event.target).closest(".clickable").each(function(){
    // "this" is your "clickable" clicked
  });
});

Unless an event handler on an element along the way does an event.stopPropagation() or return false, you'll get the click here.

Vincent Robert
@Vincent: Thanks this is an amazing reply, thanks for including information wasn't necessary but VERY useful! If you could also add the "event.stopPropagation()" fact, I would be happy to tick this answer =D
RadiantHex
I believe `.closest()` will only return one parent object, so using `.each()` would be unnecessary.
fudgey
@fudgey - That would depend on the usage, for example `$(this).plugin({ option: this.id });` couldn't be chained (`this` would be the wrong element), but lives inside the `.each()` closure nicely.
Nick Craver
@fudgey `.closest()` may actually return an empty jQuery, so using `.each()` is a shortcut to using an `if` to test the existence of the return value
Vincent Robert
+1  A: 

$('*').click( function() {...} ) will only catch clicks on elements that existed when you made the call to .click(). To catch clicks on elements that may be created later you will need to bind to body or document as others have suggested.

jasongetsdown
Thanks for this extra piece of knowledge, very valuable.
RadiantHex