Fork me on GitHub

Bootstrap switch

by Mattia Larentis (@SpiritualGuru), Peter Stein and Emanuele Marchi

Size



Dimensions can be changed too:


<input type="checkbox" checked class="switch-large">
<input type="checkbox" checked>
<input type="checkbox" checked class="switch-small">
<input type="checkbox" checked class="switch-mini">
<input type="checkbox" checked>
// Resets to the regular style
$('#dimension-switch').bootstrapSwitch('setSizeClass', '');

// Sets a mini switch
$('#dimension-switch').bootstrapSwitch('setSizeClass', 'switch-mini');

// Sets a small switch
$('#dimension-switch').bootstrapSwitch('setSizeClass', 'switch-small');

// Sets a large switch
$('#dimension-switch').bootstrapSwitch('setSizeClass', 'switch-large');

Colors



Colors can be changed too:

<input type="checkbox" checked data-on="primary" data-off="info">
<input type="checkbox" checked data-on="info" data-off="success">
<input type="checkbox" checked data-on="success" data-off="warning">
<input type="checkbox" checked data-on="warning" data-off="danger">
<input type="checkbox" checked data-on="danger" data-off="default">
<input type="checkbox" checked data-on="default" data-off="primary">
<input type="checkbox" id="change-color-switch" checked data-on="default" data-off="primary">
$('#change-color-switch').bootstrapSwitch('setOnClass', 'success');
$('#change-color-switch').bootstrapSwitch('setOffClass', 'danger');

Animation javascript



<input type="checkbox" checked data-animated="false">
// Enables animation for the selected item
$('#animated-switch').bootstrapSwitch('setAnimated', true);

// Disables animation for the selected item
$('#animated-switch').bootstrapSwitch('setAnimated', false);

Disabled

<input type="checkbox" checked disabled>

Text


<input type="checkbox" checked data-on-label="SI" data-off-label="NO">
$('#label-switch').bootstrapSwitch('setOnLabel', 'I');
$('#label-switch').bootstrapSwitch('setOffLabel', 'O');

Label Text

<input type="checkbox" checked data-text-label="TV">

HTML text

<input type="checkbox" checked data-on-label="<i class='icon-ok icon-white'></i>" data-off-label="<i class='icon-remove'></i>">

HTML text Label Icon

Standard
Font Awesome
Flat UI
<input type="checkbox" checked class="switch-large" data-label-icon="icon-fullscreen" data-on-label="<i class='icon-ok icon-white'></i>" data-off-label="<i class='icon-remove'></i>">
<input type="checkbox" checked class="switch-large" data-label-icon="icon-youtube icon-large" data-on-label="<i class='icon-thumbs-up icon-white'></i>" data-off-label="<i class='icon-thumbs-down'></i>">
<input type="checkbox" class="switch-large" checked data-label-icon="fui-video" data-on-label="<i class='fui-check icon-white'></i>" data-off-label="<i class='fui-cross'></i>">

Event handler JavaScript

$('#switch-change').on('switch-change', function (e, data) {
  var $element = $(data.el),
      value = data.value;

  console.log(e, $element, value);
});

Label Event handler JavaScript

Label 1
Label 2
<label id="label-toggle-switch">Click on this Text to change the switch state</label>
<input type="checkbox" checked />

<div id="label2-toggle-switch">
<label class="label-change-switch">Click on this Text to change the switch state
  <input type="checkbox" checked />
</label>
</div>
$('#label-toggle-switch').on('click', function(e, data) {
    $('.label-toggle-switch').bootstrapSwitch('toggleState');
});
$('.label-toggle-switch').on('switch-change', function (e, data) {
    alert(data.value);
});
$('#label2-toggle-switch').on('switch-change', function(e, data) {
    alert(data.value);
});

Toggle State javascript



Status!
ON!
Toggle me!
OFF!
$('#toggle-state-switch').bootstrapSwitch('status'); // true || false
$('#toggle-state-switch').bootstrapSwitch('toggleState');
$('#toggle-state-switch').bootstrapSwitch('setState', false); // true || false

Destroy javascript



$('#destroy-switch').bootstrapSwitch('destroy');

Create javascript



Create
$('#create-switch').bootstrapSwitch();

Disabled javascript



$('#disable-switch').bootstrapSwitch('isActive');
$('#disable-switch').bootstrapSwitch('toggleActivation');
$('#disable-switch').bootstrapSwitch('setActive', false);  // true || false

Radio javascript

<div class="form-group">
  <label for="option1">Option 1</label>
  <input id="option1" type="radio" name="radio1" value="option1">
  <label for="option2">Option 2</label>
  <input id="option2" type="radio" name="radio1" value="option2">
  <label for="option3">Option 3</label>
  <input id="option3" type="radio" name="radio1" value="option3">
</div>
$('.radio1').on('switch-change', function () {
    $('.radio1').bootstrapSwitch('toggleRadioState');
});

// or
$('.radio1').on('switch-change', function () {
    $('.radio1').bootstrapSwitch('toggleRadioStateAllowUncheck');
});

// or
$('.radio1').on('switch-change', function () {
    $('.radio1').bootstrapSwitch('toggleRadioStateAllowUncheck', false);
});

javascript (allow radios uncheck)

<div class="form-group">
    <label for="option11">Option 1</label>
    <input id="option11" type="radio" name="radio2" value="option1">
    <label for="option12">Option 2</label>
    <input id="option12" type="radio" name="radio2" value="option2" checked="checked">
    <label for="option13">Option 3</label>
    <input id="option13" type="radio" name="radio2" value="option3">
</div>
$('.radio2').on('switch-change', function () {
    $('.radio2').bootstrapSwitch('toggleRadioStateAllowUncheck', true);
});

Form - try to use tab, space and reset button

<form class="form-horizontal">
  <div class="form-group">
    <label class="control-label" for="inputEmail">Email</label>
    <input type="text" id="inputEmail" placeholder="Email">
  </div>
  <div class="form-group">
    <label class="control-label" for="notification1">Notification 1</label>
    <input id="notification1" type="checkbox">
  </div>
  <div class="form-group">
    <label class="control-label" for="notification2">Notification 2</label>
    <input id="notification2" type="checkbox">
  </div>
  <div class="form-actions">
    <button type="reset" class="btn btn-inverse">Reset</button>
  </div>
</form>

Modal

<a href="#myModal" role="button" class="btn btn-default" data-toggle="modal">Modal</a>

<div class="modal-body">
  <input type="checkbox" checked>
</div>