This time I will share jQuery Plugin and tutorial about Custom On/Off Toggle Switch Plugin For jQuery – on-off-switch.js, hope it will help you in programming stack.
on-off-switch.js is a simple to use jQuery plugin used for generating toggle switches with custom text, colors and sizes form ‘hidden’ or checkbox inputs.
How to use it:
1. Load jQuery JavaScript library and the jQuery on-off-switch.js plugin’s files into the webpage.
1 |
< script src = "//code.jquery.com/jquery-1.12.2.min.js" ></ script > |
2 |
< script src = "on-off-switch.js" ></ script > |
3 |
< script src = "on-off-switch-onload.js" ></ script > |
4 |
< link rel = "stylesheet" href = "css/on-off-switch.css" > |
2. Create a basic on / off toggle switch from <input type="checkbox">
.
1 |
< input type = "checkbox" id = "on-off-switch" name = "switch1" checked> |
3. Create a toggle switch without text and by using <input type="hidden">
instead of <input type="checkbox">
. Value=”1″ for checked, 0 for unchecked.
1 |
< input type = "hidden" id = "on-off-switch-notext" value = "0" > |
2 |
el: '#on-off-switch-notext' |
4. Create a toggle switch with custom color and size
1 |
< input type = "hidden" id = "on-off-switch-custom" value = "1" > |
02 |
el: '#on-off-switch-custom' , |
04 |
trackColorOn: '#F57C00' , |
06 |
trackBorderColor: '#555' , |
5. Listen to native checkbox updates.
1 |
< input type = "checkbox" name = "on-off-switch-demo" class = "custom-switch" id = "on-off-switch-demo" > |
2 |
< a href = "#" onclick = "$('#on-off-switch-demo').click();return false" >Execute $('#on-off-switchdemo').click()</ a > |
1 |
new DG.OnOffSwitchAuto({ |
5 |
listener: function (name, checked){ |
6 |
document.getElementById( "ELEMENT" ).innerHTML = "Switch " + name + " changed value to " + checked; |
6. All default options.
01 |
new DG.OnOffSwitchAuto({ |
06 |
trackBorderColor: undefined, |
17 |
trackColorOn: undefined, |
18 |
trackColorOff: '#EEE' , |
20 |
textColorOn: undefined, |
21 |
textColorOff: undefined, |
26 |
thumbColor: undefined, |
29 |
onOffTrackContainer: undefined, |
7. API methods.
- toggle: Toggles the checked state of the switch.
- check: Set the switch to checked.
- uncheck: Set the switch to unchecked.
- getValue: Returns true when checked, false otherwise.
Change log:
2016-08-09
- Fixed issue with scrolling
- Fixed drag event
This awesome jQuery plugin is developed by DHTMLGoodies. For more Advanced Usages, please check the demo page or visit the official website.