timmywil / jquery.onoff

Interactive, accessible toggle switches for the web.
http://timmywil.github.io/jquery.onoff/
MIT License
91 stars 22 forks source link

Not able to switch state #8

Closed renege closed 9 years ago

renege commented 9 years ago

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();
        }
    });
});
renege commented 9 years ago

The following works:

$('.onoffswitch-checkbox').attr('checked', 'checked');

But if you wanna re-toggle all to the "off-state", it doesn't work:

$('.onoffswitch-checkbox').removeAttr('checked');
timmywil commented 9 years ago

Thanks for opening an issue! You can retrieve all checked checkboxes using the :checked selector...

$('.onoffswitch-checkbox:checked').something();

To toggle checked state, it works the same as with any native checkbox. Note that the checked attribute is not the same thing as the checked property. Have a look at "Attributes vs. Properties" on this page.

renege commented 9 years ago

@timmywil,

Yes ok, but it is not working like that:

$(document).on('click', '#resetStates', function(){

    $.ajax({
        type: 'POST',
        url: '/disable-all',
        complete: function() {
            $('.onoffswitch-checkbox').removeProp('checked');
        }
    });

});

$(document).on('click', '#enableAll', function(){

    $.ajax({
        type: 'POST',
        url: '/enable-all',
        complete: function() {
            $('.onoffswitch-checkbox').prop('checked');
        }
    });

});
timmywil commented 9 years ago

Don't remove the property. Set it to false.

.prop("checked", false)