2

I've built a dropdown menu that uses a slideUp event if the menu itself or anywhere in the body is clicked:

$('html, .currentPage').click(function() { 
    $('.currentMenu').slideUp('fast', function() { myFunction(); });
});

I also have a function inside the slideUp event callback.

However, my problem is that the function is being called on html or .currentPage click whether the slideUp event has occurred or not.

How can I make it so the function(); ONLY happens if the slideUp event runs?

3 Answers 3

1
$('html, .currentPage').click(function() { 
    var $currentMenu = $('.currentMenu');

    if($currentMenu.is(':visible')) { 
        $currentMenu.slideUp('fast', function() { myFunction(); });
    }
});

even more

$('html, .currentPage').click(function() { 
    var $currentMenu = $('.currentMenu');

    if($currentMenu.is(':visible')) { 
        $currentMenu.slideUp('fast', myFunction);
    }
});

You can also optionally use $currentMenu.css('display') != 'none' instead of $currentMenu.is(':visible').

The options are suggested based on the actual behaviour of slideUp(), described in the official documentation at http://api.jquery.com/slideUp/.

Sign up to request clarification or add additional context in comments.

Comments

1
$('.currentMenu').slideToggle('fast', function() { myFunction(); });

Comments

0

You could add class open to your menu and slideUp() if hasClass('open'):

if ( $menu.hasClass('open') ) {
    $menu.slideUp('fast', function(){ });
    ...
}

3 Comments

or just $('.currentMenu').is(':visible')
Yeah, that would work too depending on how the menu is styled.
Not related to how the menu is styled. slideUp() hides the menu, so, :is('visible') is reliable if your hiding logic is slideUp(), which is defined already in the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.