Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to "click away" from a drop down menu? [duplicate]

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?

like image 591
supercoolville Avatar asked Jan 02 '12 13:01

supercoolville


1 Answers

You could perhaps use the focus event, So if its not in focus you hide it. Here is jquery's api for it

like image 123
Marthin Avatar answered Oct 15 '22 23:10

Marthin