157

How do I check if all checkboxes with class="abc" are selected?

I need to check it every time one of them is checked or unchecked. Do I do it on click or change?

Nick stands with Ukraine
  • 6,365
  • 19
  • 41
  • 49
santa
  • 11,716
  • 43
  • 149
  • 239

9 Answers9

289

I think the easiest way is checking for this condition:

$('.abc:checked').length == $('.abc').length

You could do it every time a new checkbox is checked:

$(".abc").change(function(){
    if ($('.abc:checked').length == $('.abc').length) {
       //do something
    }
});
MasterAM
  • 15,644
  • 6
  • 43
  • 63
cbrandolino
  • 5,703
  • 2
  • 18
  • 27
109
$('input.abc').not(':checked').length > 0
Naftali
  • 142,114
  • 39
  • 237
  • 299
Porco
  • 4,045
  • 2
  • 22
  • 25
  • 2
    Perfect, clean and elegant solution. "if ($('input.abc').not(":checked").length) {...}" also works. – Skorunka František Oct 21 '16 at 16:43
  • 6
    That answer not correct. It checking if there is any checkboxes checked. But question is _Check if **all** checkboxes are selected_. So the correct code is: `$('input.abc').not(':checked').length === 0`. – Artem P Jun 20 '17 at 13:37
  • 1
    This answer is better than the accepted answer if you consider performance. This will loop just once, instead of twice. – Maarten Kieft Oct 07 '17 at 18:15
  • 1
    This approach is cleaner because it does not require knowing the selector used to obtain the original jQuery collection - similarly you can use `$someCollection.is(':checked').length` for other related tests. Also see https://stackoverflow.com/questions/16576560 for notes about further optimization. – Jake Sep 25 '20 at 23:55
17

You can use change()

$("input[type='checkbox'].abc").change(function(){
    var a = $("input[type='checkbox'].abc");
    if(a.length == a.filter(":checked").length){
        alert('all checked');
    }
});

All this will do is verify that the total number of .abc checkboxes matches the total number of .abc:checked.

Code example on jsfiddle.

Mark Coleman
  • 39,942
  • 9
  • 80
  • 101
5
$('.abc[checked!=true]').length == 0
manji
  • 46,486
  • 4
  • 90
  • 101
4

Part 1 of your question:

var allChecked = true;
$("input.abc").each(function(index, element){
  if(!element.checked){
    allChecked = false;
    return false;
  } 
});

EDIT:

The (above answer) is probably better.

shaedrich
  • 4,826
  • 2
  • 26
  • 35
Dave L.
  • 9,203
  • 7
  • 41
  • 65
  • This is the most efficient answer, because it short-circuits as soon as an unchecked box is found. However, you can use `this` rather than `element` to avoid the callback requiring any explicit parameters. – Jake Sep 25 '20 at 23:56
2

A class independent solution

var checkBox = 'input[type="checkbox"]';
if ($(checkBox+':checked').length == $(checkBox).length) {
   //Do Something
}
PHPer
  • 578
  • 3
  • 19
1

Alternatively, you could have also used every():

// Cache DOM Lookup
var abc = $(".abc");

// On Click
abc.on("click",function(){

    // Check if all items in list are selected
    if(abc.toArray().every(areSelected)){
        //do something
    }

    function areSelected(element, index, array){
        return array[index].checked;
    }
});
Nick Cordova
  • 615
  • 6
  • 16
1

This is how I achieved it in my code:

if($('.citiescheckbox:checked').length == $('.citiescheckbox').length){
    $('.citycontainer').hide();
}else{
    $('.citycontainer').show();
}
Jaime Montoya
  • 5,968
  • 7
  • 60
  • 85
1

The search criteria is one of these:

input[type=checkbox].MyClass:not(:checked)
input[type=checkbox].MyClass:checked

You probably want to connect to the change event.

Steve Wellens
  • 20,326
  • 2
  • 25
  • 66