Search code examples
javascriptjqueryjquery-pluginscheckbox

Changing a Switchery checkbox state from code


How do I programatically change a jQuery Switchery checkbox state from checked to unchecked (and vice versa)?

I tried using global_switch_sound.prop('checked', true) on the checkbox element but it doesn't work.

HTML:

<input id="sound-active-input" type="checkbox"  />

JavaScript:

 global_switch_sound = new Switchery(document.querySelector('#sound-active-input'));

Solution

  • Try this:

    $('.switchery').click();
    

    It should trigger a click and switch. OR this:

    $('.switchery').trigger('click');