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?
@Adam is off just a bit
if( $group1.filter(':not(:checked)').length === 0){
//continue is OK
}
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With