Possible Duplicate:
How to detect a click outside an element?
I have a drop down menu that appears on click. When the user clicks away from it, it disappears.
For the on click Im using:
$("#title").click(function() {
dropdown_show();
);
But when the user clicks away, Im using:
$('body').click(function(e) {
if ((!$(e.target).is('#title'))&&(!$(e.target).is('#dropdown'))) {
dropdown_hide();
}
});
Is there a better way to know when a user clicks away without having to run an event every single time the user clicks on the body?
You could perhaps use the focus event, So if its not in focus you hide it. Here is jquery’s api for it