Bootstrap Tutorial - Bootstrap CheckBox








Bootstrap makes a set of checkboxes or radio buttons into a set of Bootstrap buttons that are placed alongside each other.

<!DOCTYPE html>
<html lang="en">
<head>
<script type='text/javascript'
  src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<link rel="stylesheet"
  href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet"
  href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<script
  src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script
  src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script type="text/javascript">
<!--from   w ww .java  2 s  .  c o  m-->

</script>  
</head>
  <body style='margin:30px'>
    <div class="btn-group" data-toggle="buttons">
        <label  class="btn btn-default">
            <input  type="checkbox"> Option  1
        </label>
        <label class="btn btn-default">
            <input  type="checkbox"> Option  2
        </label>
        <label  class="btn btn-default">
            <input  type="checkbox"> Option  3
        </label>
    </div>
  </body>
</html>

Click to view the demo

You can create multi-colored buttons in the button group by mixing Bootstrap's button classes, such as btn-primary, btn-info, and so on.





CheckBox Group

We can add the attribute data-toggle="buttons" to a group of checkboxes for checkbox style toggling on button groups

<!--  w w  w  .  j  a  va 2  s.  c o m-->
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<style type="text/css">
    .bs-example{
      margin: 20px;
    }
</style>
</head>
<body>
<div class="bs-example">
    <div class="btn-group" data-toggle="buttons">
        <label class="btn btn-primary">
            <input type="checkbox"> Option 1
        </label>
        <label class="btn btn-primary">
            <input type="checkbox"> Option 2
        </label>
        <label class="btn btn-primary">
            <input type="checkbox"> Option 3
        </label>
    </div>
</div>
</body>
</html>

Click to view the demo





Pre Check

Add the class .active on input's label element to pre-checked options.

<!-- w w  w  . ja  v a2 s  .co m-->
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<style type="text/css">
    .bs-example{
      margin: 20px;
    }
</style>
</head>
<body>
<div class="bs-example">
    <div class="btn-group" data-toggle="buttons">
        <label class="btn btn-primary active">
            <input type="checkbox"> Option 1
        </label>
        <label class="btn btn-primary">
            <input type="checkbox"> Option 2
        </label>
        <label class="btn btn-primary active">
            <input type="checkbox"> Option 3
        </label>
    </div>
</div>
</body>
</html>

Click to view the demo