Check if all checkboxes are selected

Jquery

Jquery Problem Overview


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?

Jquery Solutions


Solution 1 - Jquery

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
    }
});

Solution 2 - Jquery

$('input.abc').not(':checked').length > 0

Solution 3 - Jquery

You can use http://api.jquery.com/change/">`change()`</a>

$("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.

Solution 4 - Jquery

$('.abc[checked!=true]').length == 0

Solution 5 - Jquery

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.

Solution 6 - Jquery

A class independent solution

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

Solution 7 - Jquery

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.

Solution 8 - Jquery

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;
	}
});

Solution 9 - Jquery

This is how I achieved it in my code:

if($('.citiescheckbox:checked').length == $('.citiescheckbox').length){
    $('.citycontainer').hide();
}else{
    $('.citycontainer').show();
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsantaView Question on Stackoverflow
Solution 1 - JquerycbrandolinoView Answer on Stackoverflow
Solution 2 - JqueryPorcoView Answer on Stackoverflow
Solution 3 - JqueryMark ColemanView Answer on Stackoverflow
Solution 4 - JquerymanjiView Answer on Stackoverflow
Solution 5 - JqueryDave L.View Answer on Stackoverflow
Solution 6 - JqueryPHPerView Answer on Stackoverflow
Solution 7 - JquerySteve WellensView Answer on Stackoverflow
Solution 8 - JqueryNick CordovaView Answer on Stackoverflow
Solution 9 - JqueryJaime MontoyaView Answer on Stackoverflow