JQuery On/Off Switch

This scripts transforms standard html checkboxes into animated switches.

This is done by hiding the checkbox and instead displaying a custom switch widget. When the switch is checked or unchecked, the checked state of the checkbox will be updated automatically so that you can use it in form inputs as you with with a standard checkbox.

Demo

Bookmark and Share

License

Apache Free License 2.0

Download

Download on-off-switch.zip

Or from Github: https://github.com/DHTMLGoodies/jquery-on-off-switch

Examples/Demos

Manual Configuration

With custom size and color

Before and after demo:

Available Offline

Click here to transform the checkbox above.

These switches have been created automatically by class names

Option One
Option Two
Option Three
Option Four with custom html attributes

Configuration

Include files

Example:

<script type="text/javascript" src="js/jquery-1.11.2.min.js"> </script>
<script type="text/javascript" src="js/on-off-switch.js"> </script>
<link rel="stylesheet" type="text/css" href="css/on-off-switch.css"/>

Step 2: Add one or more checkboxes or hidden inputs which will be converted to a switch.

Example:

<input type="checkbox" id="id-of-checkbox" checked name="check1">
<!-- value="1" for checked, value="0" for unchecked -->
<input type="hidden" id="id-of-hidden" value="1" name="hidden">

Step3: Configure the switch

These are the options you can configure:

  • el: References to a checkbox or hidden input field, example: el:"#id-of-checkbox" for <input type="checkbox" id="id-of-checkbox"> value="1" checked>.
  • textOn: Optional text when checked
  • textOff: Optional text when unchecked
  • height: Optional height in pixels of the switch(default = 30)
  • width: Optional width in pixels of the switch. If not set, width will be calculated based on text widths(textOn, textOff)
  • trackColorOn: Track background color when checked (default = green).
  • trackColorOff: Track background color when unchecked (default = light gray).
  • textColorOn: text color of textOn.
  • textColorOff: text color of textOff.
  • listener: Optional function to call on change. Arguments: name of checkbox and checked(true|false).
    Example:
    listener:function(name, checked){ alert("Name: " + name + " - checked: " + checked); }
  • trackBorderColor: Optional border color for the background.
  • textSizeRatio: Size of text relative to height of switch (default = 0.4).

Example code:

<script type="text/javascript">
new DG.OnOffSwitch({
   el: '#id-of-checkbox',
   textOn: 'Yes',
   textOff: 'No',
   height:40,
   trackColorOn:'#F57C00',
   trackColorOff:'#666',
   textColorOff:'#fff',
   trackBorderColor:'#555'
});
</script>

Convert Multiple checkboxes.

The DG.OnOffSwitch is used to convert a single checkbox into a switch. You also have DG.OnOffSwitchAuto which is used to convert many checkboxes at once. This is how you do it:

Step 1: give all your checkboxes a css class.

Example:

<input type="checkbox" class="custom-switch" checked name="check1">
<input type="checkbox" class="custom-switch" name="check2">
<input type="checkbox" class="custom-switch" checked name="check4" data-text-on="On" data-text-off="Off">

Step 2: Call DG.OnOffSwitchAuto.

<script type="text/javascript">
   new DG.OnOffSwitchAuto({
   cls:'.custom-switch',
   textOn:'Yes',
   textOff:'No'
});
</script>

Specify the CSS class name of your checkboxes using the cls attribute. Then configure the switches using the same configuration options as for DG.OnOffSwitch shown above.

If you need individual styling/texts for some of the switches, use custom html properties like data-textOn and data-textOff, i.e. the property name prefixed by data-

Example:

<input type="checkbox" class="custom-switch" checked name="check4" data-textOn="On" data-textOff="Off">

Methods:

The following public methods are available:

  • 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.

The script will also listen to the jQuery event "click". Example:

<input type="checkbox" name="on-off-switch-demo6" class="custom-switch" id="on-off-switch-demo6">
<a href="#" onclick="$('#on-off-switch-demo6').click();return false">Execute $('#on-off-switchdemo6').click()</a>

Access a specific checkbox using code:

The automatically created checkboxes can be access with code using the syntax:

DG.switches("name-of-checkbox") // or DG.switches("#id-of-checkbox")

Where name-of-checkbox is the name attribute of the native checkbox, example:

<input type="checkbox" name="name-of-checkbox" class="custom-switch" id="id-of-checkbox">

To access it by id, use the # prefix.

Then you can call the methods above on this object:

DG.switches["#id-of-checkbox"].toggle();
DG.switches["name-of-checkbox"].check();
DG.switches["name-of-checkbox"].uncheck();

Comments

StephenLes
I tried https://greenrevolutioncbd.com/cbd-products/cbd-gummies/ as a service to the beginning mores pattern week, and I'm genuinely surprised by way of the results. I've struggled with nervousness and occasional sleeplessness, and CBD seemed to offer a nice, still efficient, quickness of stillness and relaxation. There was no 'far up,' perfectly a compassionate easing of my anxiety symptoms and a more restful night's sleep. It tasted a bit crude, but that's a mignonne trade-off as a replacement for understanding more at peace. I'm looking advance to seeing how it helps in the desire run.
StephenLes at 07:53PM, 2024/06/17.
ThomasteM
Trying [url=https://greenrevolution.com/product/melatonin-doozies/ ]sleep gummies weed[/url] for the cardinal heyday was an eye-opening experience. The fun and euphoria were unexpected but welcomed. It enhanced my sensory intuition and sparked a newfound acknowledgement in place of music and art. At any rate, the great hunger caught me mad guard. Total, it was a illustrious introduction to a late world of sensations.
ThomasteM at 07:15AM, 2024/06/20.
ThomasteM
Infuriating https://greenrevolution.com/product/watermelon/ for the sooner heyday was an eye-opening experience. The diminution and euphoria were unexpected but welcomed. It enhanced my sensory insight and sparked a newfound rise in place of music and art. Anyway, the great yearn caught me off guard. Complete, it was a illustrious introduction to a new humanity of sensations.
ThomasteM at 10:10AM, 2024/06/23.

Post your comment

Don't have an avatar? Create one at Gravatar.com.

Confirmation code:

Go to cbolson.com


About/Contact | A good idea? | Submit a script | Privacy notice
© 2005 - 2024 dhtmlgoodies.com