Categories
JavaScript Answers

How to check if all checkboxes are unchecked with JavaScript?

Spread the love

Sometimes, we want to check if all checkboxes are unchecked with JavaScript.

In this article, we’ll look at how to check if all checkboxes are unchecked with JavaScript.

How to check if all checkboxes are unchecked with JavaScript?

To check if all checkboxes are unchecked with JavaScript, we can select all the checked checkboxes with document.querySelector.

For instance, we write:

<form>
  <input type='checkbox'>
  <input type='checkbox'>
</form>

to add a form with some checkboxes.

Then we write:

const isChecked = document.querySelectorAll('input:checked').length === 0
console.log(isChecked)

to select all the checked checkboxes with document.querySelectorAll('input:checked').

Then we get the number of checked checkboxes with length.

And then we check if that is 0.

If it is, then no checkboxes are checked.

Conclusion

To check if all checkboxes are unchecked with JavaScript, we can select all the checked checkboxes with document.querySelector.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *