Categories
JavaScript Answers

How to use jQuery to check whether at least one checkbox is checked?

Spread the love

Sometimes, we want to use jQuery to check whether at least one checkbox is checked.

In this article, we’ll look at how to use jQuery to check whether at least one checkbox is checked.

How to use jQuery to check whether at least one checkbox is checked?

To use jQuery to check whether at least one checkbox is checked, we can selected the checked checkboxes and get the length of that.

For instance, we write:

<form>
  <input type="checkbox" value="true" checked="true" name="chk[120]">
  <input type="checkbox" value="true" checked="true" name="chk[128]">
  <input type="checkbox" name="chk[130]">
  <input type="checkbox" name="chk[143]">
  <input type="submit" name="btnsubmit" value="Submit">
</form>

to add a form with some checkboxes.

Then we write:

const someChecked = $('form input:checked').length > 0
console.log(someChecked)

We select the checked checkboxes with $('form input:checked').

And we use length to get the number of checked checkboxes selected.

Therefore, someChecked should be true since some checkboxes are checked.

Conclusion

To use jQuery to check whether at least one checkbox is checked, we can selected the checked checkboxes and get the length of that.

Leave a Reply

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