1

I am trying to make an onclick dropdown menu for a website. I want my .sublink elements to slide down when I click on the .dropdown element and then for the .sublink to slide back up when I click on anywhere else on the page.

The menu drops down, but it does not slide back up when I click somewhere else on the website.

Following is the code that I currently have for the menu. Can someone please help me on this? Thank you!

$(function() {
$('.dropdown').click(function() {
    $('.sublinks').stop(false, true).hide();

    var submenu = $(this).parent().next();

    submenu.css({
        position: 'absolute',
        top: $(this).offset().top + $(this).height() + 'px',
        left: $(this).offset().left + 'px',
        zIndex: 1000
    });

    submenu.stop().slideDown(300);



   //click anywhere outside the menu
   $(document).click(function() {
        var $el = $(this);
        $el.not('.dropdown') && $el.slideUp(300);
    });
});
});

3 Answers 3

1

Try to use blur event:

$('.dropdown').blur(function () {
     var $submenu = (...); // <- get submenu selector here
     $submenu.slideUp();
});
Sign up to request clarification or add additional context in comments.

1 Comment

I was going to say that it will work only for form elements (.dropdown in this case is most likely a div, ul or something like that, not a form element), but as I can see here, focus and blur events may happen on any element if it has tabindex attribute set
0

In your code, which is for click anywhere in document, this will point to a document, not to an actual element where event happened. You can modify your code like below (use e.target instead of this):

 $(function() {
    $('.dropdown').click(function() {
        $('.sublinks').stop(false, true).hide();

        var submenu = $(this).parent().next();

        submenu.css({
            position: 'absolute',
            top: $(this).offset().top + $(this).height() + 'px',
            left: $(this).offset().left + 'px',
            zIndex: 1000
        });

        submenu.stop().slideDown(300);
    });
    //click anywhere outside the menu
    $(document).click(function(e) {        
            var $el = $(e.target);
            if(!$el.hasClass('.dropdown') && $el.closest(".dropdown").length == 0 && !$el.hasClass('.sublinks') && $el.closest(".sublinks").length == 0)
                $('.sublinks:visible').slideUp(300);
     });
});

See also $el.closest(".dropdown").length == 0 it is to ensure that click happens not in child element of .dropdown

Here is a demo for it. Click in different locations and check console: http://jsfiddle.net/2ryWF/. You will find that this always points to document

5 Comments

This is the closest solution so far... but the menu is still not sliding up. Whatever I click slides up and out of the screen now, with this code. Help!
see updated code. I assumed that not will work correctly, but that is not what you need. Also, place $(document).click(function(e) { outside of $('.dropdown').click(function() {, otherwise click handler will be added to document each time you open drop down
Following is my page on jsfiddle: jsfiddle.net/2ryWF/2 Notice how anything you click on slides up and goes away; also, the dropdown menu does not go away until you click on each link, one-by-one. However, I want only the whole dropdown menu to slide up when I click anywhere else on the page.
See this code: jsfiddle.net/2ryWF/5 You had some mess with fiddle itself (HTML had some wrong JS) and I missed that slideUp should be applied to drop down actually, not to target element.
Additionally, you must check if click happens not inside .sublinks div. Otherwise dropdown will be closed each time some dropdown item is clicked
0

Looks like your hook for click anywhere is inside the dropdown click hook, try something like this(not tested);

$(function() {
//click on menu(dropdown div)
    $('.dropdown').click(function() {
        $('.sublinks').stop(false, true).hide();

        var submenu = $(this).parent().next();

        submenu.css({
            position: 'absolute',
            top: $(this).offset().top + $(this).height() + 'px',
            left: $(this).offset().left + 'px',
            zIndex: 1000
        });

        submenu.stop().slideDown(300);
    });

//click anywhere in document
   $(document).click(function() {
        var $el = $(this);
        $el.not('.dropdown') && $el.slideUp(300);
    });
});

Comments

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.