The title sums it up.
I simply want to hide #test when anything else than #test or its children is clicked.
Thanks a bunch!
The title sums it up.
I simply want to hide #test when anything else than #test or its children is clicked.
Thanks a bunch!
You can use event bubbling to your advantage here, for example:
$("#test").click(function(e) {
e.stopPropagation();
});
$(document).click(function() {
$("#test").hide();
});
If the click came from within #test
it stops bubbling up via event.stopPropagation()
, if it came from anywhere else the click
will (by default) bubble all the way up to document
, which has a .click()
handler to hide #test
.
Maybe something like :
// http://api.jquery.com/delegate/
// http://api.jquery.com/not-selector/
$("body").delegate("*:not(#test)", "click", function () {
$("#test").hide(); // But would be better to cache $("#test") !
});