How to Change a Switchery checkbox state with JavaScript?

Sometimes, we want to change a Switchery checkbox state with JavaScript.

In this article, we’ll look at how to change a Switchery checkbox state with JavaScript.

Change a Switchery checkbox state with JavaScript

To change a Switchery checkbox state with JavaScript, we just call the click method on the Switchery checkbox element.

For instance, we write:

<script src="https://cdnjs.cloudflare.com/ajax/libs/switchery/0.8.2/switchery.min.js"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/switchery/0.8.2/switchery.css" />

<input class='switchery' type='checkbox'>

to add the Switchery script tag and CSS file link tag.

Then we add the checkbox input element.

Next, we write:

document.querySelector('.switchery').click();

to select the Switchery checkbox with:

document.querySelector('.switchery')

Then we call click on that to click on the checkbox to make it checked.

Conclusion

To change a Switchery checkbox state with JavaScript, we just call the click method on the Switchery checkbox element.