How to hide Twitter Bootstrap dropdown

JqueryTwitter Bootstrap

Jquery Problem Overview


This is getting annoying — when I click on an item in a Bootstrap dropdown, the dropdown doesn't close. I have it set up to open a Facebox lightbox when you click the dropdown item but there is a problem with it.

enter image description here


What I have tried

When the item is clicked, I tried doing this:

    $('.dropdown.open').removeClass('open');
    $('.dropdown-menu').hide();

That hides it, but then for some reason it won't open again.

As you can see I really need the dropdown to close, because it looks crappy when it stays open (mainly because the z-index of the dropdown is higher than the Facebox modal box overlay.


Why I'm not using Bootstrap's built-in modal box

If you're wondering why I'm not using the nice-looking modal box built into Bootstrap, it is because:

  1. It doesn't have a way to load content into it with AJAX.
  2. You have to type HTML each time for the modal; with Facebox you can do a simple: $.facebox({ajax:'/assets/ajax/dialogs/dialog?type=block-user&id=1234567'});
  3. It uses CSS3 animations to animate (which looks very nice) but in non-CSS3 browsers it just shows, which doesn't look that nice; Facebox uses JavaScript to fade in so it works in all browsers.

Jquery Solutions


Solution 1 - Jquery

Try this:

$('.dropdown.open .dropdown-toggle').dropdown('toggle');

This may also work for you:

$('[data-toggle="dropdown"]').parent().removeClass('open');

Solution 2 - Jquery

tryed out most the options from here but none of them realy worked for me. (the $('.dropdown.open').removeClass('open'); did hide it, but if you moused over before clicking anything else it showed up again.

so i endet up doing it whith a $("body").trigger("click")

Solution 3 - Jquery

$('.open').removeClass('open');

Bootstrap 4 (October 2018):

$('.show').removeClass('show');

Solution 4 - Jquery

You only need to do $('.dropdown.open').removeClass('open'); remove second line which hides the dropdown menu.

Solution 5 - Jquery

This can be done without writing JavaScript code by adding attribute data-toggle="dropdown" into anchor tag as shown below:

https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">

Solution 6 - Jquery

This is what worked for me:

$(this).closest(".dropdown").find(".dropdown-toggle").dropdown("toggle");

Solution 7 - Jquery

The selected answer didn't work for me, but I'm thinking it's because of the newer version of Bootstrap. I ended up writing this:

$('.btn-navbar').addClass('collapsed');
$('.nav-collapse').removeClass('in').css('height', '0');

Hopefully that's helpful to someone else in the future.

Solution 8 - Jquery

Try this!

.removeClass("open").trigger("hide.bs.dropdown").trigger("hidden.bs.dropdown");

OR

$(clicked_element).trigger("click");

It always work for me.

Solution 9 - Jquery

Why use my method, because if the user gets out of the div, this metod allows the user a certain delay before the submenus dissapear. It's just like using the superfish plugin.

  1. First download hover intent javascript

Link here : Hover intent javascript

2)Here is my code to execute

$(document).ready(function(){

        var config = {    
            over: showBootrapSubmenu,   
            timeout: 500,
            out: hideBootrapSubmenu  
        };

        function showBootrapSubmenu(){  
             $(this).addClass('open');
        }
        function hideBootrapSubmenu(){  
          $(this).removeClass('open');
            }

        //Hover intent for Submenus
        $(".dropdown").hoverIntent(config);

    });

Solution 10 - Jquery

$('.dropdown.open').removeClass('open');

Solution 11 - Jquery

Reading the documentation and using JavaScript it could be done using the toggle method:

$('.button-class').on('click',function(e) {
    e.preventDefault();
    $('.dropdown-class').dropdown('toggle');
}

You can read about it in: http://getbootstrap.com/javascript/#dropdowns-methods

Solution 12 - Jquery

It may have been added in retrospect (though, that feature is not documented, even in Bootstrap 4.3.1), but you can simply call it with a hide parameter. Just make sure to call it on the toggler element. Like so:

$('.dropdown-toggle').dropdown('hide');
// or
$('[data-toggle="dropdown"]').dropdown('hide');

Of course, this also works in reverse with show.

Solution 13 - Jquery

Try to open HTML with Bootstrap Modal, I use this code:

$(function() {
     $('a[data-toggle=modal]').click(function(e) {
          e.preventDefault();
          var page = $(e.target).attr('href');
          var url = page.replace('#','');
          $(page).on('show', function () {
              $.ajax({
              url: "/path_to/"+url+".php/html/...",
              cache: false,
              success: function(obj){
                   $(page).css('z-index', '1999');
                   $(page).html(obj);
              }
              });
         })
     });
}); 

and the link is like this:

<a href="#my_page" data-toggle="modal">PAGE</a>

Solution 14 - Jquery

Here is my solution on how to close the button dropdown and toggle the button :

$(".btn-group.open", this)
    .trigger("click")
    .children("a.dropdown-toggle")
    .trigger("blur")

Where "this" is a global container of the button group.

Solution 15 - Jquery

Bootstrap 4.1:

$(".dropdown-link").closest(".dropdown-menu").removeClass('show');

Solution 16 - Jquery

Selecting by attribute is the slow way. Here is the fastest solution to hide the opened dropdown:

$(".dropdown-menu.show").parent().dropdown("toggle");

or use following, if the .dropdown-menu is not the next children element (find the closest parent dropdown control):

$(".dropdown-menu.show").closest(".dropdown").dropdown("toggle");

Solution 17 - Jquery

this worked for me, i had a navbar and several pulldown menus.

$(document).ready(function () {
    $('a.dropdown-toggle').each(function(){
        $(this).on("click", function () {
            $('li.dropdown').each(function () {
                $(this).removeClass('open');
            });
        });
    });
});

Solution 18 - Jquery

The easiest way to do this is: you add a hide label:

<label class="hide_dropdown" display='hide'></label>

then everytime you want to hide dropdown, you call:

$(".hide_dropdown").trigger('click');

Solution 19 - Jquery

Don't know if this will help anyone (maybe it is too specific to my case, and not to this question) but for me this worked:

$('.input-group.open').removeClass('open');

Solution 20 - Jquery

After click:

// Hide mobile menu
$('.in,.open').removeClass('in open');
// Hide dropdown
$('.dropdown-menu').css('display','none');
setTimeout(function(){
	$('.dropdown-menu').css('display','');
},100);

Solution 21 - Jquery

Use class="dropdown-toggle" on the anchor tag, then when you click on the anchor tag it will close the bootstrap dropdown.

Solution 22 - Jquery

Hey this simple jQuery trick should help.

$(".dropdown li a").click(function(){
$(".dropdown").removeClass("open");
});

Solution 23 - Jquery

The easist way:

  $('.navbar-collapse a').click(function(){
    $('.navbar-toggle').trigger('click')
  })

Solution 24 - Jquery

you can use this code in your current event handler

$('.in,.open').removeClass('in open');

in my scenario I used when ajax call complete

jQuery(document).on('click', ".woocommerce-mini-cart__buttons .wc-forward.checkout", function() {
    var _this = jQuery(this);
    ajax_call(_this.attr("data-ajax-href"), "checkout-detail");
    $('.in,.open').removeClass('in open');
    return false;
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNathanView Question on Stackoverflow
Solution 1 - JqueryBart WegrzynView Answer on Stackoverflow
Solution 2 - JqueryVeXiiView Answer on Stackoverflow
Solution 3 - Jquerykuiro5View Answer on Stackoverflow
Solution 4 - JqueryGurinderView Answer on Stackoverflow
Solution 5 - JqueryShivaji MutkuleView Answer on Stackoverflow
Solution 6 - JqueryDan PuzaView Answer on Stackoverflow
Solution 7 - JqueryscoljaView Answer on Stackoverflow
Solution 8 - Jquerybbc_danangView Answer on Stackoverflow
Solution 9 - JquerySylvainView Answer on Stackoverflow
Solution 10 - JqueryJared ChristensenView Answer on Stackoverflow
Solution 11 - JqueryAlfcheeView Answer on Stackoverflow
Solution 12 - JqueryWoodrowShigeruView Answer on Stackoverflow
Solution 13 - JqueryAlex BallView Answer on Stackoverflow
Solution 14 - JqueryIgor Aloise LodView Answer on Stackoverflow
Solution 15 - JquerykennykeeView Answer on Stackoverflow
Solution 16 - JqueryAleksey KuznetsovView Answer on Stackoverflow
Solution 17 - JquerydetayView Answer on Stackoverflow
Solution 18 - JqueryzhouwubaiView Answer on Stackoverflow
Solution 19 - JqueryArekView Answer on Stackoverflow
Solution 20 - JqueryBrynner FerreiraView Answer on Stackoverflow
Solution 21 - JqueryZeeshan JanView Answer on Stackoverflow
Solution 22 - JquerySalil ChhetriView Answer on Stackoverflow
Solution 23 - JquerysfyView Answer on Stackoverflow
Solution 24 - JqueryQaisar irfanView Answer on Stackoverflow