Interactive, accessible toggle switches for the web.

Overview

On-Off Toggle Switch

Build Status

Interactive, accessible toggle switches for the web

Transform checkboxes into toggle switches.

Toggle Switch

Toggle switches made for the web are often solely constructed to change when clicked or tapped, animating side-to-side as they toggle. That's a good start, but some people like to slide toggle switches. Javascript is used for creation and for adding the sliding functionality, but the toggle is purely CSS.

Customize the look of your toggle switch using proto.io's generator.

Browser support

Supported browsers are the same as those supported by jQuery 2. IE6-8 are not supported.

However, note that IE9 does not support CSS transitions. The toggle switches, but does not animate.

Pointer, touch, and mouse events are supported.

Getting Started

Download the production version (4kb) or the development version and grab the CSS.

With AMD

define([ 'jquery', 'plugins/jquery.onoff' ], function( $ ) {
  $('input[type="checkbox"]').onoff();
});

With Script Tags

<script src="jquery.js"></script>
<script src="plugins/jquery.onoff.min.js"></script>
<script>
jQuery(function($) {
  $('input[type="checkbox"]').onoff();
});
</script>

Examples

All you need is a checkbox:

<input type="checkbox" />

However, you can also start with the generated HTML to avoid FOUC.

<div class="onoffswitch">
  <input type="checkbox" class="onoffswitch-checkbox" id="myonoffswitch" />
  <label class="onoffswitch-label" for="myonoffswitch">
    <span class="onoffswitch-inner"></span>
    <span class="onoffswitch-switch"></span>
  </label>
</div>

Then initialize onoff:

$('input[type=checkbox]').onoff();

Options

OnOff.defaults = {
  // The event namespace
  // Should always be non-empty
  // Used to bind jQuery events without collisions
  namespace: '.onoff',

  // The class added to the checkbox
  className: 'onoffswitch-checkbox'
};

Methods

All methods can be called widget-style.

option()

Returns: If getting, the option value; if setting, the jQuery collection for chaining.

The option() method can be called with no arguments to return all options.

var options = $input.onoff('option');

Pass a key to retrieve a single option.

var eventNamespace = $input.onoff('option', 'namespace');

Pass a key and value or an object of key/value pairs to set options.

$input.onoff('option', 'namespace', 'newspace');
$input.onoff('option', {
  namespace: 'newspace',
  className: 'newclass'
});

disable()

Returns: jQuery for chaining

Disables the OnOff instance and unbinds all events.

$input.onoff('disable');

isDisabled()

Returns: Boolean

Returns whether the OnOff instance is currently disabled.

var disabled = $input.onoff('isDisabled');

enable()

Returns: jQuery for chaining

Enables the OnOff instance, rebinding events and ensuring the proper HTML.

$input.onoff('enable');

wrap()

Returns: jQuery for chaining

Ensures the HTML for the toggle switch is correct. This method only adds any missing HTML.

$input.onoff('wrap');

unwrap()

Returns: jQuery for chaining

Removes HTML related to OnOff, leaving only the checkbox.

$input.onoff('unwrap');

destroy()

Returns: jQuery for chaining

Disables the OnOff instance and removes data, but does not call unwrap().

$input.onoff('destroy');

instance()

Returns: OnOff

Returns the OnOff instance.

var instance = $input.onoff('instance');

Release History

  • 0.4.0 10/30/2014 Corrected content inside label element
  • 0.3.6 6/23/2014 Updated package.json
  • 0.3.5 5/12/2014 Fixed regression with iOS devices
  • 0.3.4 3/26/2014 Update pointertouch
  • 0.3.3 3/11/2014 Fire change event when checked is changed async
  • 0.3.2 3/11/2014 Container now inherits classes from the checkbox
  • 0.3.1 3/3/2014 Minor pointertouch update
  • 0.3.0 3/3/2014 Update to full-blown pointertouch
  • 0.2.4 3/3/2014 Integrate jquery.event.pointertouch into build.
  • 0.1.0 1/15/2014 First release

Acknowledgements

Thanks go out to the team at proto.io and their elegant CSS.

License

Copyright (c) 2014 Timmy Willison. Licensed under the MIT license.

Comments
  • Not able to switch state

    Not able to switch state

    I have multiple checkboxes with the plugin enabled. I wanna do something like;

    $(document).on('click', '#everything', function(){
    
        $.ajax({
            type: 'POST',
            url: '/toggle-all',
                    complete: function() {
                        // toggle all to active state....
                       alert(' all checkboxes  active in state');
                        $('.onoffswitch-checkbox').something();
            }
        });
    });
    
    opened by renege 4
  • Dragging to switch button does not work on iPad.

    Dragging to switch button does not work on iPad.

    Good initiative to write this plugin.

    We are facing issue on iPad, when try to switch button from NO state to YES state. i.e. dragging handle left to right. Handle bounce back to left or does not move at all.

    However http://timmywil.github.io/jquery.onoff/ example works fine on ipad.

    Would please help me out whats going wrong. Here Codepen : http://cdpn.io/obrna

    opened by pramodchoudhari 3
  • proto.io generated html is wrong

    proto.io generated html is wrong

    The generated html from http://proto.io/freebies/onoff/ is providing the following snippet:

    <div class="onoffswitch">
        <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch">
        <label class="onoffswitch-label" for="myonoffswitch">
            <span class="onoffswitch-inner"></span>
            <span class="onoffswitch-switch"></span>
        </label>
    </div>
    

    where the correct one (also mentioned in your README) is:

    <div class="onoffswitch">
      <input type="checkbox" class="onoffswitch-checkbox" id="myonoffswitch" />
      <label class="onoffswitch-label" for="myonoffswitch">
        <div class="onoffswitch-inner"></div>
        <div class="onoffswitch-switch"></div>
      </label>
    </div>
    

    The difference is the div instead of span inside the label that prevented the plugin to properly figure out how to render itself.

    opened by linakis 2
  • Bump lodash from 2.4.2 to 4.17.15

    Bump lodash from 2.4.2 to 4.17.15

    Bumps lodash from 2.4.2 to 4.17.15.

    Release notes

    Sourced from lodash's releases.

    4.0.0

    lodash v4.0.0

    2015 was big year! Lodash became the most depended on npm package, passed 1 billion downloads, & its v3 release saw massive adoption!

    The year was also one of collaboration, as discussions began on merging Lodash & Underscore. Much of Lodash v4 is proofing out the ideas from those discussions. Lodash v4 would not be possible without the collaboration & contributions of the Underscore core team. In the spirit of merging our teams have blended with several members contributing to both libraries.

    For 2016 & lodash v4.0.0 we wanted to cut loose, push forward, & take things up a notch!

    Modern only

    With v4 we’re breaking free from old projects, old environments, & dropping old IE < 9 support!

    4 kB Core

    Lodash’s kitchen-sink size will continue to grow as new methods & functionality are added. However, we now offer a 4 kB (gzipped) core build that’s compatible with Backbone v1.2.4 for folks who want Lodash without lugging around the kitchen sink.

    More ES6

    We’ve continued to embrace ES6 with methods like _.isSymbol, added support for cloning & comparing array buffers, maps, sets, & symbols, converting iterators to arrays, & iterable _(…).

    In addition, we’ve published an es-build & pulled babel-plugin-lodash into core to make tree-shaking a breeze.

    More Modular

    Pop quiz! 📣

    What category path does the bindAll method belong to? Is it

    A) require('lodash/function/bindAll') B) require('lodash/utility/bindAll') C) require('lodash/util/bindAll')

    Don’t know? Well, with v4 it doesn’t matter because now module paths are as simple as

    var bindAll = require('lodash/bindAll');
    

    We’ve also reduced module complexity making it easier to create smaller bundles. This has helped Lodash adoption with libraries like Async & Redux!

    1st Class FP

    With v3 we introduced lodash-fp. We learned a lot & with v4 we decided to pull it into core.

    Now you can get immutable, auto-curried, iteratee-first, data-last methods as simply as

    var _ = require('lodash/fp');
    var object = { 'a': 1 };
    </tr></table> ... (truncated)
    
    Commits

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot ignore this [patch|minor|major] version will close this PR and stop Dependabot creating any more for this minor/major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot use these labels will set the current labels as the default for future PRs for this repo and language
    • @dependabot use these reviewers will set the current reviewers as the default for future PRs for this repo and language
    • @dependabot use these assignees will set the current assignees as the default for future PRs for this repo and language
    • @dependabot use this milestone will set the current milestone as the default for future PRs for this repo and language

    You can disable automated security fix PRs for this repo from the Security Alerts page.

    dependencies 
    opened by dependabot[bot] 1
  • Change switch sate does not set checked state.

    Change switch sate does not set checked state.

    I tried the master version and the online sample. When I click the toggle, the checked state does not change in HTML. I set $input.attr('checked', true), the toggle does not change either.

    opened by Artoria-0x04 1
  • Minimal problem with the callbacks

    Minimal problem with the callbacks

    Hey, thanks at all for this cool thing, my problem is followed. Every time I try to get a callback, the console gave me that issue: Uncaught ReferenceError: $input is not defined

    I did all that in the tut from Github, but doesnt work. In your example HTML-File also... Hope you can help me as soon as possible.

    Regards muuvmuuv

    pending 
    opened by muuvmuuv 1
  • Update dependencies to enable Greenkeeper 🌴

    Update dependencies to enable Greenkeeper 🌴

    Let’s get started with automated dependency management for jquery.onoff :muscle:

    This pull request updates all your dependencies to their latest version. Having them all up to date really is the best starting point for keeping up with new releases. Greenkeeper will look out for further dependency updates and make sure to handle them in isolation and in real-time, but only after you merge this pull request.

    Important: Greenkeeper will only start watching this repository’s dependency updates after you merge this initial pull request.


    💥 Tests on this branch are failing. Here’s how to proceed.

    To solve the issue, first find out which of the dependency’s updates is causing the problem. Then fix your code to accomodate the changes in the updated dependency. next-update is a really handy tool to help you with this.

    Then push your changes to this branch and merge it.

    🏷 How to check the status of this repository

    Greenkeeper adds a badge to your README which indicates the status of this repository.

    This is what your badge looks like right now :point_right: Greenkeeper badge

    🙈 How to ignore certain dependencies

    You may have good reasons for not wanting to update to a certain dependency right now. In this case, you can change the dependency’s version string in the package.json file back to whatever you prefer.

    To make sure Greenkeeper doesn’t nag you again on the next update, add a greenkeeper.ignore field to your package.json, containing a list of dependencies you don’t want to update.

    // package.json
    {
      …
      "greenkeeper": {
        "ignore": [
          "package-names",
          "you-want-me-to-ignore"
        ]
      }
    }
    
    👩‍💻 How to update this pull request
      # Change into your repository’s directory
      git fetch --all
      git checkout greenkeeper/initial
      npm install-test
      # Adapt your code until everything works again
      git commit -m 'chore: adapt code to updated dependencies'
      git push https://github.com/timmywil/jquery.onoff.git greenkeeper/initial
    
    ✨ How do dependency updates work with Greenkeeper?

    After you merge this pull request, Greenkeeper will create a new branch whenever a dependency is updated, with the new version applied. The branch creation should trigger your testing services and check whether your code still works with the new dependency version. Depending on the the results of these tests Greenkeeper will try to open meaningful and helpful pull requests and issues, so your dependencies remain working and up-to-date.

    -  "underscore": "^1.6.0"
    +  "underscore": "^1.7.0"
    

    The above example shows an in-range update. 1.7.0 is included in the old ^1.6.0 range, because of the caret ^ character. When the test services report success Greenkeeper will silently delete the branch again, because no action needs to be taken – everything is fine.

    However, should the tests fail, Greenkeeper will create an issue to inform you about the problem immediately.

    This way, you’ll never be surprised by a dependency breaking your code. As long as everything still works, Greenkeeper will stay out of your way, and as soon as something goes wrong, you’ll be the first to know.

    -  "lodash": "^3.0.0"
    +  "lodash": "^4.0.0"
    

    In this example, the new version 4.0.0 is not included in the old ^3.0.0 range. For version updates like these – let’s call them “out of range” updates – you’ll receive a pull request.

    This means that you no longer need to check for new versions manually – Greenkeeper will keep you up to date automatically.

    These pull requests not only serve as reminders to update: If you have solid tests and good coverage, and the pull requests passes those tests, you can very likely just merge it and release a new version of your software straight away :shipit:

    To get a better idea of which ranges apply to which releases, check out the extremely useful semver calculator provided by npm.

    FAQ and help

    There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


    Good luck with your project and see you soon :sparkles:

    Your Greenkeeper bot :palm_tree:

    greenkeeper 
    opened by greenkeeper[bot] 0
  • Bump lodash from 2.4.2 to 4.17.19

    Bump lodash from 2.4.2 to 4.17.19

    Bumps lodash from 2.4.2 to 4.17.19.

    Release notes

    Sourced from lodash's releases.

    4.17.16

    4.0.0

    lodash v4.0.0

    2015 was big year! Lodash became the most depended on npm package, passed 1 billion downloads, & its v3 release saw massive adoption!

    The year was also one of collaboration, as discussions began on merging Lodash & Underscore. Much of Lodash v4 is proofing out the ideas from those discussions. Lodash v4 would not be possible without the collaboration & contributions of the Underscore core team. In the spirit of merging our teams have blended with several members contributing to both libraries.

    For 2016 & lodash v4.0.0 we wanted to cut loose, push forward, & take things up a notch!

    Modern only

    With v4 we’re breaking free from old projects, old environments, & dropping old IE < 9 support!

    4 kB Core

    Lodash’s kitchen-sink size will continue to grow as new methods & functionality are added. However, we now offer a 4 kB (gzipped) core build that’s compatible with Backbone v1.2.4 for folks who want Lodash without lugging around the kitchen sink.

    More ES6

    We’ve continued to embrace ES6 with methods like _.isSymbol, added support for cloning & comparing array buffers, maps, sets, & symbols, converting iterators to arrays, & iterable _(…).

    In addition, we’ve published an es-build & pulled babel-plugin-lodash into core to make tree-shaking a breeze.

    More Modular

    Pop quiz! 📣

    What category path does the bindAll method belong to? Is it

    A) require('lodash/function/bindAll') B) require('lodash/utility/bindAll') C) require('lodash/util/bindAll')

    Don’t know? Well, with v4 it doesn’t matter because now module paths are as simple as

    var bindAll = require('lodash/bindAll');
    

    We’ve also reduced module complexity making it easier to create smaller bundles. This has helped Lodash adoption with libraries like Async & Redux!

    1st Class FP

    With v3 we introduced lodash-fp. We learned a lot & with v4 we decided to pull it into core.

    Now you can get immutable, auto-curried, iteratee-first, data-last methods as simply as

    Commits
    Maintainer changes

    This version was pushed to npm by mathias, a new releaser for lodash since your current version.


    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot use these labels will set the current labels as the default for future PRs for this repo and language
    • @dependabot use these reviewers will set the current reviewers as the default for future PRs for this repo and language
    • @dependabot use these assignees will set the current assignees as the default for future PRs for this repo and language
    • @dependabot use this milestone will set the current milestone as the default for future PRs for this repo and language

    You can disable automated security fix PRs for this repo from the Security Alerts page.

    dependencies 
    opened by dependabot[bot] 0
Owner
Timmy Willison
jQuery Core Team Lead, Front-End Lead at Spokestack
Timmy Willison
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
iOS 7 style switches for your checkboxes

Description Switchery is a simple component that helps you turn your default HTML checkbox inputs into beautiful iOS 7 style switches in just few simp

Alexander Petkov 2.1k Dec 31, 2022
Chrome extension that switches default build tool to Maven at start.spring.io

start.spring.io default to Maven On the 18th of October 2022 https://start.spring.io switched the default build tool from Maven to Gradle spring-io/st

Maciej Walkowiak 11 Dec 14, 2022
A package to toggle properties of your HTML tags.

Toggler A package(atleast the code) to toggle properties of tags. I mostly use toggle classes while making a switch theme method, button interaction e

chandra sekhar pilla 6 Jan 9, 2022
DoMe is a ToDo App. you can add, delete and reorder elements of the todo list using drag and drop. You can also toggle between dark&light mode

DO ME Todo App Live Preview : DO ME Built With : - ReactJS - TailwindCSS Make sure you have: - Git - Nodejs version 14 or higher (we recommend using

Medjahdi Islem 5 Nov 18, 2022
Toggle Dark-Mode manually on InStream.de

InStream.de Dark-Mode Umschalter Was es macht Dieses Add-On für Firefox fügt einen Button hinzu, mit dem man websites zwischen "Normal" und "Dark-Mode

InStream.de 2 Feb 7, 2022
❤️ A heart-shaped toggle switch component for React.

heart-switch A heart-shaped toggle switch component for React. Inspired by Tore Bernhoft's I heart toggle Dribbble shot. ?? Table of Contents ?? Getti

Anatoliy Gatt 413 Dec 15, 2022
A utility for creating toggleable items with JavaScript. Inspired by bootstrap's toggle utility. Implemented in vanillaJS in a functional style.

LUX TOGGLE Demo: https://jesschampion.github.io/lux-toggle/ A utility for creating toggleable dom elements with JavaScript. Inspired by bootstrap's to

Jess Champion 2 Oct 3, 2020
🤖 Tailwind CSS assistant helps you to edit classes (includes JIT & ignores purge), toggle breakpoint classes on an element and view current breakpoint

Tailwind CSS Assistant See it in action on this example website ?? ✅ Small JavaScript package that helps you work with Tailwind CSS by... Showing you

Mark Mead 30 Dec 28, 2022
An Awesome Toggle Menu created with HTML,CSS,JQuery,font-awesome and line by line comment.

Demo : https://blackx-732.github.io/AwesomeMenu/ Under open source license No ©copyright issues Anyone can be modify this code as well Specifically we

BlackX-Lolipop 2 Feb 9, 2021
Pure CSS toggle switch

Pure CSS toggle switch demo install npm i toggle-switch-css or yarn add toggle-switch-css use <label class="toggle-switch my-toggle-switch"> <in

magic-akari 6 Sep 14, 2021
Kuldeep 2 Jun 21, 2022
A tiny JavaScript library to easily toggle the state of any HTML element in any contexts, and create UI components in no time.

A tiny JavaScript library to easily toggle the state of any HTML element in any contexts, and create UI components in no time. Dropdown, navigation bu

Matthieu Bué 277 Nov 25, 2022
Toggle the theme in Obsidian's panels

Obsidian Theme Toggler Toggle the theme individually for each of Obsidian's panels. What the plugin does It adds a theme toggle button to each pane in

Lars Magnus Klavenes 13 Nov 28, 2022
A pure CSS toggle switch for form input checkboxes

Toggle Switchy A pure CSS toggle switch for form input checkboxes Preview Installation CSS <link rel="stylesheet" href="toggle-switchy.css"> HTML <lab

Adam Culpepper 34 Dec 8, 2022
Listing of accessible components & patterns

Accessible Components I've built a good handful of accessible markup patterns and widgets at this point. Each is based on testing with users, UX and d

Scott O'Hara 538 Jan 3, 2023
An open-source boat display cockpit for navigation, speed, heading, and tide tables running on Raspberry Pi and accessible as a webapp through any smartphone.

An open-source boat display cockpit for navigation, speed, heading, and tide tables running on Raspberry Pi and accessible as a webapp through any smartphone

Andy 44 Dec 30, 2022