How to hide Twitter Bootstrap dropdown
JqueryTwitter BootstrapJquery 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.
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:
- It doesn't have a way to load content into it with AJAX.
- 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'});
- 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.
- 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;
});