Bootstrap Tutorial - Create switch








The following code shows how to create switch.

Example

<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-2.1.0.js'></script>
<script type='text/javascript'
  src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<link rel="stylesheet" type="text/css"
  href="http://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.0.1/css/bootstrap3/bootstrap-switch.css">
<link rel="stylesheet" type="text/css"
  href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<script type='text/javascript'
  src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<script type='text/javascript'
  src="http://cdnjs.cloudflare.com/ajax/libs/bootstrap-switch/3.0.1/js/bootstrap-switch.js"></script>
<script type='text/javascript'>
$(window).load(function(){<!-- w ww  .  ja  va  2  s . c o m-->
    $('.BSswitch').bootstrapSwitch('state', true);
    
    $('#CheckBoxValue').text($("#TheCheckBox").bootstrapSwitch('state'));
    
    $('#TheCheckBox').on('switchChange.bootstrapSwitch', function () {
        $("#CheckBoxValue").text($('#TheCheckBox').bootstrapSwitch('state'));
    });
    
    $('.probeProbe').bootstrapSwitch('state', true);
    
    $('.probeProbe').on('switchChange.bootstrapSwitch', function (event, state) {
        console.log(this);
        console.log(event);
        console.log(state);
    });
    $('#toggleSwitches ').click(function () {
        $('.BSswitch ').bootstrapSwitch('toggleDisabled');
        if ($('.BSswitch ').attr('disabled')) {
            $(this).text('Enable All Switches ');
        } else {
            $(this).text('Disable All Switches ');
        }
    });
});
</script>
</head>
<body style='margin:30px'>
  <input id="TheCheckBox" 
         type="checkbox" 
         data-off-text="False"
       data-on-text="True" 
       checked="false" 
       class="BSswitch">
  <label>This Switch is Set to 
      <label id="CheckBoxValue" value="None"></label>
  </label>
  <br />
  <input id="TheCheckBox2" name="GroupedSwitches" class="BSswitch"
    type="radio" data-off-text="False" data-on-text="True" checked="false">
  <input id="TheCheckBox3" name="GroupedSwitches" class="BSswitch"
    type="radio" data-off-text="False" data-on-text="True" checked="false">
  <input id="TheCheckBox4" name="GroupedSwitches" class="BSswitch"
    type="radio" data-off-text="False" data-on-text="True" checked="false">
  <br />
  <br />
  
  <button type="button" id="toggleSwitches">Disable All Switches</button>
  <br />
  <br />
  <div class="make-switch">
    <input type="checkbox" checked="true" class="probeProbe" />
  </div>
</body>
</html>

Click to view the demo