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.
EugeneClany
Exploring [url=https://rvfhemp.com/pages/where-can-i-buy-bulk-hemp-flowers ]buy bulk hemp flower[/url] and pre roll weed has been an enlightening voyage object of me. The je sais quoi and smell of these products are stirring, oblation a calming and enjoyable experience. Whether I'm unwinding after a crave period or seeking resourceful incentive, hemp pre-rolls plan for a understandable choice that I can trust. The convenience of pre-rolls combined with the benefits of hemp flowers occasion them a go-to voice instead of off and mindfulness. I know the r‚clame to detail and honesty of the products, enhancing my comprehensive satisfaction. Greatly advocate stressful hemp flowers and pre-rolls in place of a holistic and fulfilling trial!
EugeneClany at 12:15PM, 2024/08/03.
EugeneClany
Exploring https://rvfhemp.com/products/sweet-harmony-pre-rolls and pre roll weed has been an enlightening trip instead of me. The worth and odour of these products are exciting, gift a calming and enjoyable experience. Whether I'm unwinding after a covet date or seeking original ardour, hemp pre-rolls produce a common additional that I can trust. The convenience of pre-rolls combined with the benefits of hemp flowers make them a go-to choice for off and mindfulness. I perceive the publicity to detail and honesty of the products, enhancing my comprehensive satisfaction. Exceptionally advocate troublesome hemp flowers and pre-rolls for a holistic and fulfilling sagacity!
EugeneClany at 11:52PM, 2024/08/05.
RichardDus
Disquieting [url=https://www.nothingbuthemp.net/pages/saint-paul-location ]saint paul cannabis[/url] has been somewhat the journey. As someone keen on usual remedies, delving into the world of hemp has been eye-opening. From THC tinctures to hemp seeds and protein powder, I've explored a type of goods. Teeth of the misunderstanding surrounding hemp, researching and consulting experts own helped pilot this burgeoning field. Overall, my sophistication with hemp has been positive, sacrifice holistic well-being solutions and sustainable choices.
RichardDus at 04:16PM, 2024/09/19.
OrlandoNeuch
I recently reach-me-down this area to identify https://synergy3.com/hvac/ductless/ , and I couldn’t be happier with the results. The search was straightforward, and I appreciated the particularized profiles and buyer reviews on the side of each contractor. It made comparing options and reading nearly other clients' experiences easy. The contractors I contacted were coax, licensed, and offered competitive quotes. This position is a spectacular resource in behalf of anyone needing reliable haven vamp services. Highly recommended payment its understandable interface and worth listings!
OrlandoNeuch at 06:26PM, 2024/09/22.
Scottlep
Disquieting https://www.nothingbuthemp.net/collections/thc-beverages has been somewhat the journey. As someone pointed on natural remedies, delving into the world of hemp has been eye-opening. From THC tinctures to hemp seeds and protein pulverize, I've explored a variety of goods. Teeth of the misunderstanding adjoining hemp, researching and consulting experts own helped cross this burgeoning field. Comprehensive, my sophistication with hemp has been favourable, offering holistic well-being solutions and sustainable choices.
Scottlep at 11:28PM, 2024/10/02.

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