Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checked state for buttons Bootstrap

I want to have a checked state for group chexboxes in Bootstrap 3.0.2. docs

html:

<div class="btn-group" data-toggle="buttons">
        <label class="btn btn-default">
        <input type="checkbox" name="123" data-toggle="button"> <span class="glyphicon glyphicon-star"></span> 123
      </label>
      <label class="btn btn-default">
        <input type="checkbox" name="456"> <span class="glyphicon glyphicon-star-empty"></span> 456
      </label>
    </div>

But data-toggle="button" doesnt works. jsfiddle

How to fix it? Thanks.

like image 701
Lev Avatar asked Nov 10 '13 15:11

Lev


People also ask

How do you checkbox is checked or not in Bootstrap?

$(document). on('change','#briskbusiness',function() { if(this. checked){ alert(); } });

How do I toggle a button in Bootstrap?

Add data-toggle="buttons" to a . btn-group containing those modified buttons to enable their toggling behavior via JavaScript and add . btn-group-toggle to style the <input> s within your buttons. Note that you can create single input-powered buttons or groups of them.

Why use bootstrap button styles?

You can use Bootstrap’s custom button styles for actions in forms, dialogs, and more with support for multiple sizes, states, and more... Bootstrap includes several predefined button styles, each serving its own semantic purpose, with a few extras thrown in for more control.

How to enable toggling of buttons in Bootstrap?

Bootstrap's .button styles may possibly be applied to additional elements, just like <label> - s, to generate checkbox or radio style button toggling. Add data-toggle=" buttons" to .btn-group consisting of those customized buttons to allow toggling in their various styles.

How do I toggle a button’s active state?

Add data-toggle="button" to toggle a button’s active state. If you’re pre-toggling a button, you must manually add the .active class and the attribute aria-pressed="true" to the <button>. Bootstrap’s .button styles can be applied to other elements, such as <label> s, to provide checkbox or radio style button toggling.

What is the difference between active and disabled button in Bootstrap?

A button can be set to an active (appear pressed) or a disabled (unclickable) state: The class .active makes a button appear pressed, and the class .disabled makes a button unclickable: For a complete reference of all button classes, go to our complete Bootstrap Button Reference.


2 Answers

To actually check the input, you will need to add the checked property. This will not actually make it appear checked, but is important if you are using this in a form and actually want the input to be checked by default.

<input type="checkbox" name="123" data-toggle="button" checked>

To make it look checked (or pressed), add class .active to the .btn label wrapping it.

 <label class="btn btn-default active">

http://jsfiddle.net/ZktYG/2/

Not sure why data-toggle="buttons" isn't working. Could be related to this: https://github.com/twbs/bootstrap/issues/8816

For now, you can achieve the same effect through JS by doing something like:

$('.btn-group').on('input', 'change', function(){
   var checkbox = $(this);
   var label = checkbox.parent('label');
   if (checkbox.is(':checked'))  {
      label.addClass('active');
   }
   else {
      label.removeClass('active');
   }
});
like image 193
Michelle Avatar answered Sep 30 '22 06:09

Michelle


It's been a couple of years; however, I wanted to address the actual problem above of why the data-toggle="button" wasn't working.

The answer was to remove the data-toggle="button" on the <input>'s.

<div class="btn-group" data-toggle="buttons">
    <label class="btn btn-default">
        <input type="checkbox" name="123"> 123
    </label>
    <label class="btn btn-default">
        <input type="checkbox" name="456"> 456
    </label>
</div>
like image 39
Austin Hanson Avatar answered Sep 30 '22 06:09

Austin Hanson