Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide Twitter Bootstrap dropdown

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.
like image 521
Nathan Avatar asked Jun 08 '12 00:06

Nathan


People also ask

How do I hide a drop down menu in Bootstrap?

Usage. Via data attributes or JavaScript, the dropdown plugin toggles hidden content (dropdown menus) by toggling the . show class on the parent list item.

How do I hide the dropdown arrow in Bootstrap?

Every Bootstrap dropdown button or link has an ::after selector in CSS. ::after selector is often used to insert some text after the content of the element. In this case, the content is a dropdown arrow. To remove it, just make the content go 'none'.

How do I remove the drop down toggle icon?

In Bootstrap 4, you can remove the dropdown arrow which is called caret by declaring a $enable-caret SASS variable and setting it to false : $enable-caret: false; This overrides the default value of true set in the Bootstrap's _variable.

How do I stop dropdown menu Close menu items on clicking inside?

As all Twitter Bootstrap users know, the dropdown menu closes on click (even clicking inside it). To avoid this, I can easily attach a click event handler on the dropdown menu and simply add the famous event. stopPropagation() .


1 Answers

Try this:

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

This may also work for you:

$('[data-toggle="dropdown"]').parent().removeClass('open'); 
like image 197
Bart Wegrzyn Avatar answered Sep 18 '22 23:09

Bart Wegrzyn