Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if all checkboxes within jQuery object are checked, return as boolean

Hey all. I've been been trying to figure this out for a while now.

I create a jQuery object of checkboxes and store it in a variable:

$group1 = $('#checkbox1,#checkbox2,#checkbox3,#checkbox4');

The user cannot continue unless all checkboxes in that group are checked.

I've been using an if statement combined with .is(':checked') to find a boolean value:

if( $group1.is(':checked') ){
  //continue is OK
}

...but .is(':checked') will return TRUE if any checkboxes are checked within the group. Essentially, .is(':checked') performs an OR operation on the selected elements in $group1. I'm looking for an AND operation, so all selected elements must be checked to return TRUE. Is there a jQuery function that does this, or another workaround?

like image 256
neveraskedforthis Avatar asked Sep 15 '10 16:09

neveraskedforthis


2 Answers

@Adam is off just a bit

if( $group1.filter(':not(:checked)').length === 0){
  //continue is OK
}
like image 166
BBonifield Avatar answered Sep 20 '22 15:09

BBonifield


Corrected:

You could filter to get only the elements that are not checked, and then check to see if any are any elements still in the collection, if there are not than all the elements in the group are checked:

if( $group1.filter(':not(:checked)').length === 0){
  //continue is OK
}
like image 37
Adam Avatar answered Sep 20 '22 15:09

Adam