Catching any click performed using jQuery

RadiantHex picture RadiantHex · Jul 18, 2010 · Viewed 32.8k times · Source

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. =)

Answer

Nick Craver picture Nick Craver · Jul 18, 2010

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.