views:

45

answers:

2

Basically I have code that checks if any checkboxes with a given class are selected. If one of those isn't, another checkbox doesn't get selected (parent).

Logic

for each checkbox
{
if this isn't checked then uncheck checkbox A
if all siblings aren't checked uncheck checkbox A
// recursion for grandparents as well.
}

Is there a Any() function or All() function in jQuery to do this?

Any help would be greatly appreciated.

Regards, Jamie

A: 

Why don't you do it like this:

when a chebox changes state, count all the checkboxes, and count all the checked checkboxes, and then compare the value's. if they are the same, then they are all checked ...

Nealv
I thought of this just after I did the question! Typical.
Jamie
var allSelected = $('.className').not(':checked').length == 0 is probably better.
Jamie
Happens to everybody, I always immediately ask someone else, because I know the moment I do I come up with an idea ;)
Nealv
A: 
$(':checkbox:not(:checked)').each(function() {

});

I don't get the internal logic. Checkbox A is some other checkbox in which we don't know the state. You want to uncheck it if any checkbox is unchecked, then you want to uncheck it again if any of its siblings aren't checked as well?

Are we talking about your standard 'select all' checkbox or some sort of tree?

if($(':checkbox:not(#checkbox_A):not(:checked)').length > 0) {
    $('#checkbox_A').attr('checked', '');
}
Neil
var allSelected = $('.className').not(':checked').length == 0;
Jamie