In the Bootstrap dropdown event section:
hide.bs.dropdown . This event is fired immediately after the hide instance method is called.
To begin with, to prevent the popup menu, we can just listen to this event and not continue it by returning false :
$('#myDropdown').on('hide.bs.dropdown', function () { return false; });
For a complete solution, you probably want it to close when the dropdown list itself clicks. Therefore, only for some time we want the window to not close.
To do this, we will set the .data() flags in two more events raised by the drop-down menu:
shown.bs.dropdown . When shown, we will set .data('closable') to falseclick - when clicking the .data('closable') button, set true
Thus, if the hide.bs.dropdown event was raised by click in the drop-down list, we will allow closing.
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 that I added the keep-open class to the drop-down list)
<div class="dropdown keep-open "> <button id="dLabel" role="button" href="#" class="btn btn-primary" data-toggle="dropdown" data-target="#" > Dropdown <span class="caret"></span> </button> <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>
KyleMit Nov 05 '13 at 19:48 2013-11-05 19:48
source share