This is an easy question. I am using bootstrap v3.2.0 to creating a list of checkboxes on a navbar dropdown menu.
I was just wondering if there is a way of not snapping the checkbox away after clicking on each checkbox.
I have provided the fiddle: http://jsfiddle.net/D2RLR/5649/
<div class="input-group">
<div class="input-group-btn">
<button tabindex="-1" class="btn btn-default" type="button">Select</button>
<button tabindex="-1" data-toggle="dropdown" class="btn btn-default dropdown-toggle" type="button">
<span class="caret"></span>
</button>
<ul role="menu" class="dropdown-menu">
<li><a href="#">
<input type="checkbox"><span class="lbl"> Every day</span>
</a></li>
<li class="divider"></li>
<li><a href="#">
<input type="checkbox">
<span class="lbl"> Monday</span>
</a></li>
<li><a href="#">
<input type="checkbox"><span class="lbl">
Tuesday</span>
</a></li>
<li><a href="#">
<input type="checkbox"><span class="lbl">
Wednesday</span>
</a></li>
<li><a href="#">
<input type="checkbox"><span class="lbl">
Thursday</span>
</a></li>
<li><a href="#">
<input type="checkbox"><span class="lbl">
Friday</span>
</a></li>
<li><a href="#">
<input type="checkbox"><span class="lbl">
Saturday</span>
</a></li>
<li><a href="#">
<input type="checkbox"><span class="lbl">
Sunday</span>
</a></li>
<li class="divider"></li>
<li><a href="#">
<input type="checkbox"><span class="lbl"> Last Weekday in month</span>
</a></li>
</ul>
</div>
<input type="text" class="form-control">
</div>
Will accept the first answer that works correctly.
Removing the data attribute data-toggle="dropdown" and implementing the open/close of the dropdown can be a solution.
DropDownList displays checkboxes to the left of each item when you set showCheckbox property to true. It allows you to select more than one item at a time from DropDownList. Popup list stays open until the user finishes selection.
You just need to prevent event bubbling:
$('.dropdown-menu').click(function(e) {
e.stopPropagation();
});
Where dropdown-menu
is a class of your dropdown, you can specify another or use id instead. The reason why it works is that Bootrstrap listens click event on root element body
in order to close popup when you click on the page. Due to event bubbling click event propagates up the DOM tree to the very body
unless you stop it from doing it with stopPropagation
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With