134

I know how to see if an individual checkbox is selected or not.

But Im having trouble with the following - given a form id I need to see if any of the checkboxes are selected (i.e 1 or more), and I need to see if none are selected. Basically I need two separate functions that answer these two questions. Help would be appreciated. Thanks!

Actually, I would just need a function to tell me if none are selected. Knowing this would answer the other question.

marcgg
  • 65,020
  • 52
  • 178
  • 231
bba
  • 14,621
  • 11
  • 29
  • 26

8 Answers8

271

You can use something like this

if ($("#formID input:checkbox:checked").length > 0)
{
    // any one is checked
}
else
{
   // none is checked
}
rahul
  • 184,426
  • 49
  • 232
  • 263
  • 9
    `$("#formID input:checkbox:checked").length` would be sufficient here, too – Damon Oct 11 '13 at 20:30
  • @Damon I guess you meant `if ($("#formID input:checkbox:checked").length){}` (without the `>0`) would be sufficient because 0 is a falsey value, see http://james.padolsey.com/javascript/truthy-falsey/ – Adriano May 19 '14 at 12:32
  • 12
    jQuery says regarding the `:checkbox` selector: `For better performance in modern browsers, use [type="checkbox"]`, see http://api.jquery.com/checkbox-selector/ - same for radiobuttons btw, `use [type="radio"] rather than :radio` http://api.jquery.com/radio-selector/ – Adriano May 19 '14 at 13:05
31

JQuery .is will test all specified elements and return true if at least one of them matches selector:

if ($(":checkbox[name='choices']", form).is(":checked"))
{
    // one or more checked
}
else
{
    // nothing checked
}
Michael Logutov
  • 2,551
  • 4
  • 28
  • 32
  • Althought `is()` seems to work, having `:checked` directly in the selector as indicated in @rahul's answer seems more appropriate. is() seems more useful when "inside callbacks", see http://api.jquery.com/is/. Or am I missing something? – Adriano May 19 '14 at 12:26
  • No, it's pretty much what written in docs - you check if an element is matching specified attribute. Applying it's as filter and then checking if you get at least one item in result is the same but imo longer and not so expressive. – Michael Logutov May 20 '14 at 06:22
  • +1 for the fact that `.is(":checked")` in your solution is more expressive, not sure about the rest though. – Adriano May 21 '14 at 13:59
  • Namely `$("form input[type=checkbox]").is(":checked")` might be a simpler & more generic approach. – Adriano May 21 '14 at 15:17
  • @AdrienBe Using `is` might be better performance, because it stops as soon as it finds one. – ChrisW Aug 25 '16 at 16:56
10

You can do this:

  if ($('#form_id :checkbox:checked').length > 0){
    // one or more checkboxes are checked
  }
  else{
   // no checkboxes are checked
  }

Where:

  • :checkbox filter selector selects all checkbox.
  • :checked will select checked checkboxes
  • length will give the number of checked ones there
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • jQuery says regarding the `:checkbox` selector: `For better performance in modern browsers, use [type="checkbox"]`, see http://api.jquery.com/checkbox-selector/ – Adriano May 19 '14 at 13:04
9

Without using 'length' you can do it like this:

if ($('input[type=checkbox]').is(":checked")) {
      //any one is checked
}
else {
//none is checked
}
Acheme Paul
  • 1,194
  • 15
  • 19
7

This is what I used for checking if any checkboxes in a list of checkboxes had changed:

$('input[type="checkbox"]').change(function(){ 

        var itemName = $('select option:selected').text();  

         //Do something.

});     
James Drinkard
  • 15,342
  • 16
  • 114
  • 137
4

Rahul's answer is best fit for your question. Anyway, if you have a group of checkboxes to be checked and not all the checkbox in your form, you can go for it.

Put a classname for all the checkboxes you want to check, say for example, a classname test_check and now you can check if any of the checkbox is checked belonging to the group by:

$("#formID .test_check:checked").length > 0

If it returns true, assume that one or more checkboxes are checked having the classname test_check and none checked if returns false.

Hope it helps someone. Thanks :)-

Rajesh Omanakuttan
  • 6,788
  • 7
  • 47
  • 85
3

You can do a simple return of the .length here:

function areAnyChecked(formID) {
  return !!$('#'+formID+' input[type=checkbox]:checked').length;
}

This look for checkboxes in the given form, sees if any are :checked and returns true if they are (since the length would be 0 otherwise). To make it a bit clearer, here's the non boolean converted version:

function howManyAreChecked(formID) {
  return $('#'+formID+' input[type=checkbox]:checked').length;
}

This would return a count of how many were checked.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
2

This is the best way to solve this problem.

  if($("#checkbox").is(":checked")){

  // Do something here /////

  };
  • Hey, welcome to StackOverflow. Perhaps you can describe your answer a little more, as it is not apparent why it is the "best way" to solve the problem. – dddJewelsbbb Jul 14 '20 at 17:11