v2.0.1
All the options are accepted only using data-*
attributes on the element.
2 | checked
, disabled
and readonly
are exceptions to the rule, being
3 | default HTML input attributes.
4 | Therefore, there is not any way to specify the options in JavaScript during initialization.
Name | Type | Description | Values | Default |
---|---|---|---|---|
state | Boolean | The checkbox state | true, false | 'checked' attribute or true |
size | String | The checkbox state | '', 'mini', 'small', 'normal', 'large' | '' |
animate | Boolean | Animate the switch | true, false | true |
disabled | Boolean | Disable state | true, false | 'disabled' attribute or false |
readonly | Boolean | Readonly state | true, false | 'readonly' attribute or false |
on | String | Color of the left side of the switch | 'primary', 'info', 'success', 'warning', 'danger', 'default' | null |
off | String | Color of the right side of the switch | 'primary', 'info', 'success', 'warning', 'danger', 'default' | null |
on-label | String | Text of the left side of the switch | String | 'ON' |
off-label | String | Text of the right side of the switch | String | 'OFF' |
label-text | String | Text of the center handle of the switch | String | ' ' |
label-icon | String | Text of the center handle of the switch. Use to include external services icons | String | null |
Name | Description | Accepted Values | Returned Values |
---|---|---|---|
state | Get checkbox state | true, false | |
setState | Set checkbox state | (value: true, false)[, skip: true, false] | jQuery Object (input element) |
toggleState | Toggle checkbox state | [skip: true, false] | jQuery Object (input element) |
toggleRadioState | Toggle radio state | [skip: true, false] | jQuery Object (input element) |
toggleRadioStateAllowUncheck | Toggle radio state allowing uncheck of the radio input | [uncheck: true, false | skip: true, false] | jQuery Object (input element) |
setSizeClass | Set the size of the switch | '', 'mini', 'small', 'normal', 'large' | jQuery Object (input element) |
setAnimated | Animate the switch | true, false | jQuery Object (input element) |
isDisabled | Get disabled state | true, false | |
setDisabled | Set disable state | true, false | jQuery Object (input element) |
toggleDisabled | Toggle disabled state | jQuery Object (input element) | |
isReadOnly | Get Readonly state | true, false | |
setReadOnly | Set Readonly state | true, false | jQuery Object (input element) |
toggleReadOnly | Toggle readonly state | jQuery Object (input element) | |
setOnClass | Color of the left side of the switch | 'primary', 'info', 'success', 'warning', 'danger', 'default' | jQuery Object (input element) |
setOffClass | Color of the right side of the switch | 'primary', 'info', 'success', 'warning', 'danger', 'default' | jQuery Object (input element) |
setOnLabel | Text of the left side of the switch | String | jQuery Object (input element) |
setOffLabel | Text of the right side of the switch | String | jQuery Object (input element) |
setTextLabel | Text of the center handle of the switch | String | null |
setTextIcon | Text of the center handle of the switch. Use to include external services icons | String | null |
destroy | Destroy the instance of Bootstrap Switch | jQuery Object (input element) |
The only event triggered is switch-change
. It returns two parameters: event
and
5 | data
.
6 | The latter is an object that includes el
(the input DOM element) and value
(the
7 | new input state)
All the events are namespaced, therefore always append .bootstrapSwitch
when you
2 | attach your handlers.
3 | You can register to the emitted events as follows:
$('input[name="my-checkbox"]').on('switchChange.bootstrapSwitch', function(event, state) {
4 | console.log(this); // DOM element
5 | console.log(event); // jQuery event
6 | console.log(state); // true | false
7 | });
Name | Description | Parameters |
---|---|---|
init | Triggered on initialization. 'this' refers to the DOM element. | event (jQuery Event object) 8 | state (true | false) |
switchChange | Triggered on switch state change. 'this' refers to the DOM element. | event (jQuery Event object), 9 | state (true | false) |
Turn checkboxes and radio buttons into toggle switches
Currently v3.3.4 · Compatible with Bootstrap 2 and 3
Include the dependencies: jQuery, Bootstrap and Bootstrap Switch CSS + Javascript.
[...]
2 | <link href="bootstrap.css" rel="stylesheet">
3 | <link href="bootstrap-switch.css" rel="stylesheet">
4 | <script src="jquery.js"></script>
5 | <script src="bootstrap-switch.js"></script>
6 | [...]
Add your checkbox.
<input type="checkbox" name="my-checkbox" checked>
Initialize Bootstrap Switch.
$("[name='my-checkbox']").bootstrapSwitch();
Enjoy.