How to bind event to hide menu

Go To StackoverFlow.com

0

I have created a small plugin to show a dropdown menu when a div is clicked.

Below is the code to get it to work

$('#clickable_div').click(function() {
    $('#nav_menu').showMenu({
        parent:'#clickable_div' 
    });                
});

Jsfiddle with the plugin : http://jsfiddle.net/HtNK3/

So now basically i want to add a functionality where i need to hide the dropdown div when user clicks anywhere in the document but not on the dropdown div which is open

How can this be achieved ?

2012-04-05 02:03
by user1184100


2

I am using something like this:

//Close popup menus by clicking anywhere
$("body").mouseup(function(event){
        if (!$(event.target).hasClass("YOUR_CLASS_HERE")) {
        $("#nav_menu").hide();
    } 
});
2012-04-05 02:13
by Ilia Frenkel
Hi llia i tried this
if (!$(event.target).hasClass("z")) { $("#nav_menu").hide(); } but this doesn't seem to wor - user1184100 2012-04-05 02:23
If you only have one element to hide you can try this instead: if ($(event.target).id != "nav_menu")Ilia Frenkel 2012-04-05 02:41
No llia there will be multiple dropdown's : - user1184100 2012-04-05 02:52
I doesn't work on jsfiddle for whatever reason. Try it locally - Ilia Frenkel 2012-04-05 03:01


0

I would use setTimout when the mouse leaves the dropdown (mouseleaveevent) and then hide the div. Pretty common and easy.

Buggy but basic example: http://jsfiddle.net/HtNK3/5/

2012-04-05 02:06
by binarious
thanks for reply binarious..but i wanted only on clic - user1184100 2012-04-05 02:13