Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep Bootstrap Dropdown Open When Clicked Off

If the dropdown is visible, and I click outside the dropdown it closes. I need it to not close.

From the documentation:

When opened, the plugin also adds .dropdown-backdrop as a click area for closing dropdown menus when clicking outside the menu.

What JavaScript can I add to prevent the drop down from closing?

like image 711
kleban Avatar asked Nov 02 '13 08:11

kleban


People also ask

How do I keep my bootstrap dropdown open?

Just wrap the contents of the menu in a form tag. That's it.

How do I stop a dropdown from closing on click?

Removing the data attribute data-toggle="dropdown" and implementing the open/close of the dropdown can be a solution.

How do I stop bootstrap dropdown from closing?

By choosing false, the dropdown menu can only be toggled by clicking on the dropdown button. the inside makes the dropdown disappear only by choosing a menu item and the outside closes the dropdown menu only by clicking outside.

How do I keep a drop down menu open in HTML?

Use any element to open the dropdown menu, e.g. a <button>, <a> or <p> element. Use a container element (like <div>) to create the dropdown menu and add the dropdown links inside it. Wrap a <div> element around the button and the <div> to position the dropdown menu correctly with CSS.


1 Answers

From the events section of the Bootstrap dropdown documentation:

hide.bs.dropdown: This event is fired immediately when the hide instance method has been called.

For starters, to prevent the dropdown from closing, we can just listen to this event and stop it from proceeding by returning false:

$('#myDropdown').on('hide.bs.dropdown', function () {     return false; }); 

For a complete solution, you probably want to allow it to close when the dropdown itself is clicked. So only some of the time we'll want to prevent the box from closing.

To do this we'll set .data() flags in two more events raised by the dropdown:

  • shown.bs.dropdown - When shown, we'll set .data('closable') to false
  • click - When clicked, we'll set .data('closable') to true

Thus, if the hide.bs.dropdown event was raised by a click on the dropdown, we'll allow a close.

Live Demo in jsFiddle

JavaScript

$('.dropdown.keep-open').on({     "shown.bs.dropdown": function() { this.closable = false; },     "click":             function() { this.closable = true; },     "hide.bs.dropdown":  function() { return this.closable; } }); 

HTML (note I've added the class keep-open to the dropdown)

<div class="dropdown keep-open">     <!-- Dropdown Button -->     <button id="dLabel" role="button" href="#" class="btn btn-primary"             data-toggle="dropdown" data-target="#" >         Dropdown <span class="caret"></span>     </button>      <!-- Dropdown Menu -->     <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">         <li><a href="#">Action</a></li>         <li><a href="#">Another action</a></li>         <li><a href="#">Something else here</a></li>         <li class="divider"></li>         <li><a href="#">Separated link</a></li>     </ul> </div> 
like image 136
KyleMit Avatar answered Sep 21 '22 14:09

KyleMit