GithubHelp home page GithubHelp logo

JS Callback for toggle changing about ratchet HOT 4 CLOSED

twbs avatar twbs commented on July 24, 2024
JS Callback for toggle changing

from ratchet.

Comments (4)

fat avatar fat commented on July 24, 2024

this should be all event based. we can fire a toggle event on the window tho:

element.addEventListener('toggle', function () {});

from ratchet.

fat avatar fat commented on July 24, 2024

This event also bubbles, so you can add it directly to the toggle element, or just bind to the body to catch all toggles.

element.addEventListener('toggle', function (e) {
  console.log(e.target, e.detail.isActive);
});

i'm not crazy about the "isActive" name for whether you're toggling it to an on or off state… if you guys have a better name idea, i'd be open to that!

from ratchet.

dhg avatar dhg commented on July 24, 2024

SWEET

from ratchet.

abrambailey avatar abrambailey commented on July 24, 2024

Hope this helps someone else.

HTML

    <input style="visibility:hidden" type="checkbox" value="1" checked="checked"  id="user_remember_me">

    <div class="toggle" data-matcher='user_remember_me'>
      <div class="toggle-handle"></div>
    </div>

JS

  //Turns on toggle switches for any checked boxes

  var active_ids = [];
  $("input:checkbox(:checked)").each(function(){ active_ids.push($(this).attr("id")) });
  $.each( active_ids, function( key, value ) {
    $('div').find("[data-matcher='" + value + "']").addClass('active');
  });

  //Maps toggle changes to the hidden checkbox

  $('.toggle').on('click', function() {
    var inactive_ids = [];
    $('.toggle').not('active').each(function(){ inactive_ids.push($(this).attr("data-matcher")) });
    $.each( inactive_ids, function( key, value ) {
      $('#' + value).prop('checked', false);
    });
    var active_ids = [];
    $('.toggle.active').each(function(){ active_ids.push($(this).attr("data-matcher")) });
    $.each( active_ids, function( key, value ) {
      $('#' + value).prop('checked', true);
    });
  });

from ratchet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.