Bootstrap Switch Button

Bootstrap Switch Button is a widget that converts plain checkboxes into responsive switch buttons.

Installation


CDN

<link href="https://cdn.jsdelivr.net/gh/gitbrent/bootstrap-switch-button@1.1.0/css/bootstrap-switch-button.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/gh/gitbrent/bootstrap-switch-button@1.1.0/dist/bootstrap-switch-button.min.js"></script>

Download

GitHub Latest release

NPM

npm install bootstrap-switch-button

Yarn

yarn install bootstrap-switch-button

Demos


Sizes

Bootstrap Switch Button is available in different sizes.

Custom Sizes

Bootstrap Switch Button can handle custom sizes by data-width and data-height options.

Colors

Bootstrap Switch Button implements all standard bootstrap 4 button colors.

Outline Colors

Bootstrap Switch Button implements all standard bootstrap 4 button outline colors.

Dark Theme Colors

Bootstrap Switch Button colors look great on dark backgrounds.

Dark Theme Outline Colors

Bootstrap Switch Button outline colors look great on dark backgrounds.

Custom Style

Style the buttons to fit an existing UX.

Custom Text

The text can be changed easily with attributes or options.

HTML, Icons, Images

You can easily add icons or images since html is supported for on/off text.

Multiple Lines of Text

Toggles with multiple lines will adjust its heights.

Animation Speed

Transition speed can be easily controlled with css transition property on .switch-group. You can also turn animation off completely.

Stacked checkboxes

Simply add data-toggle="switchbutton" to convert checkboxes into toggles.

Inline Checkboxes

Simply add data-toggle="switchbutton" to a convert checkboxes into switch buttons.

Usage


Initialize with HTML

Simply add data-toggle="switchbutton" to convert checkboxes into switch buttons.

Initialize with JavaScript

Simply call the switchButton method to convert checkboxes into switch button.

document.getElementById('chkToggle2').switchButton();

API


Options

Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-, as in data-onlabel="Enabled".

Name Type Default Description
onlabel string | html "On" Text of the on switch label.
offlabel string | html "Off" Text of the off switch label.
onstyle string "primary" Style of the on switch.
Possible values are: primary, secondary, success, danger, warning, info, light, dark
offstyle string "light" Style of the off switch.
Possible values are: primary, secondary, success, danger, warning, info, light, dark
size string null Size of the switch button. If set to null, button is default/normal size.
Possible values are: lg, sm, xs
style string null Appends the provided value to the switch's class attribute. Use this to apply custom styles to the switch.
width integer null Sets the width of the switch.
If set to null, width will be calculated.
height integer null Sets the height of the switch.
If set to null, height will be calculated.

Methods

Methods can be used to control a switch button directly.

Method Example Description Demo
initialize document.getElementById('switch-demo').switchButton() Initializes the switch-button with options
destroy document.getElementById('switch-demo').switchButton('destroy') Destroys the switch-button
on document.getElementById('switch-demo').switchButton('on') Sets the switch-button to 'On' state
off document.getElementById('switch-demo').switchButton('off') Sets the switch-button to 'Off' state
toggle document.getElementById('switch-demo').switchButton('toggle') Toggles the state of the switch-button
enable document.getElementById('switch-demo').switchButton('enable') Enables the switch-button
disable document.getElementById('switch-demo').switchButton('disable') Disables the switch-button

Events


Event Propagation

Note All events are propagated to and from input element to the switch-button.

You should listen to events from the <input type="checkbox"> directly rather than look for custom events.

Stopping Event Propagation

Passing true to the on/off methods will enable the silent option to prevent the control from propagating the change event in cases where you want to update the controls on/off state, but do not want to fire the onChange event.

API Input

This also means that using the API or Input to trigger events will work both ways.