Bootstrap: Radio button and checkbox items in dropdown menu

Created on 20 Feb 2012  路  7Comments  路  Source: twbs/bootstrap

Please add styles for displaying radio buttons and checkboxes in dropdown menu.

Most helpful comment

A slight improvement to above answers...

<ul class="dropdown-menu dropdown-menu-form">
  <li><label class="checkbox"><input type="checkbox">One</label></li>
  <li><label class="checkbox"><input type="checkbox">Two</label></li>
</ul>
// Allow Bootstrap dropdown menus to have forms/checkboxes inside, 
// and when clicking on a dropdown item, the menu doesn't disappear.
$(document).on('click', '.dropdown-menu.dropdown-menu-form', function(e) {
  e.stopPropagation();
});

All 7 comments

There should be no reason you cannot do that right now as the styles already exist in the forms section for a block of radios or checkboxes. The only problem you'll run into is clicks closing the dropdown menu, but we dont' currently support that.

Here is my workaround for this:

I added the class "drop-menu-form" to the dropdown-menu so that you now have :

<ul class="dropdown-menu dropdown-menu-form">
    ... stuff here ...
</ul>

Then I added the following javascript

$('.dropdown-menu').on('click', function(e){
        if($(this).hasClass('dropdown-menu-form')){
            e.stopPropagation();
        }
});

Another solution if you add the dropdown dynamically:

$(document).on("click", ".dropdown-multiselect", function (e) {
    var parent = $(this).parent(".btn-group");
    if (!parent.hasClass("open"))
        parent.addClass("open");
});

In AngularJS, I worked around this by using ng-show/hide, and font awesome checkmarks to display checkboxes. Looks very nice. I'm not using a form for submit, so this won't work in that context.

    <ul class="dropdown-menu">
        <li>
            <a ng-click="filter.clearPartyFilter()" >
                <i class="icon icon-check-empty" ng-show="filter.partyFiltering"></i>
                <i class="icon icon-check-sign" ng-hide="filter.partyFiltering"></i>
                <strong>Show all parties</strong>
            </a>
        </li>
        ....
    </ul>    

A slight improvement to above answers...

<ul class="dropdown-menu dropdown-menu-form">
  <li><label class="checkbox"><input type="checkbox">One</label></li>
  <li><label class="checkbox"><input type="checkbox">Two</label></li>
</ul>
// Allow Bootstrap dropdown menus to have forms/checkboxes inside, 
// and when clicking on a dropdown item, the menu doesn't disappear.
$(document).on('click', '.dropdown-menu.dropdown-menu-form', function(e) {
  e.stopPropagation();
});

Any update on an official solution that does not require a workaround?

I'm having a hard time getting these suggestions to work on mobile browsers fyi.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  3Comments

devfrey picture devfrey  路  3Comments

IamManchanda picture IamManchanda  路  3Comments

cvrebert picture cvrebert  路  3Comments

steve-32a picture steve-32a  路  3Comments