Hide dropdown on 'body' click produces to need double click to show again


I have this drop down menu that when one menu is clicked if there was another one active, this one is deactivated and then the new one is activated:

$('.idiomas span.current').toggle(function(){
                            $('.idiomas span.clicked').next('ul').hide();
                            $('.idiomas span.clicked').removeClass('clicked');
                            $('.idiomas span.current:not(.clicked)').show();

Ok, until here is working fine, but then I wanted the menus to be hidden when user clicks outside the entire menu; figured out that on body click should work as I needed:

    $('body').bind('click', function(e) {
        if($(e.target).closest('.idiomas ul').length == 0) {
            // se ha clickado fuera del menu
            $('.idiomas ul').hide();
            $('.idiomas span.current').removeClass('clicked').show();
            //dentro: no hacemos nada.

and it does hide, but I need to click twice in the next menu item to make its sub-menu visible,

Any idea why?


Here’s something that might help. Here is the JSFiddle for it: http://jsfiddle.net/aTqP5/

Basic gist to your problem is that toggle has 2 states, and when you click on the body, the element gets hidden but the toggle state is still in “show” and has to go to “hide” then back to “show” again, hence the clicking twice.

    $('.idiomas span.current').click(function(event){
        if ( $(this).hasClass('clicked') ) {
        } else {
            $('.idiomas span.clicked').removeClass('clicked').siblings('ul').hide();

    $('body').click(function(event) {
        if ( $('.idiomas span.clicked').length > 0 ) {
            $('.idiomas ul').hide();
            $('.idiomas span.current').removeClass('clicked');

Answered By – Calvin

Answer Checked By – Clifford M. (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.