tags:

views:

65

answers:

2

Can you run a check to see if a checkbox is the only value checked against a group?

$("#ID").change(function() {
if ($("#checkboxA").is(":checked") && NOTHING ELSE IS CHECKED) {
 //do somethinge
} else {
//do something else
}
});

I need help with the "&& NOTHING ELSE IS CHECKED" and Im still not sure this would be a change function

+4  A: 

You could check that only one checkbox is checked and that checkbox has the id of the one you are looking for. Working Demo

$("#ID").change(function() {
    var checked = $("input:checkbox:checked");
    if (checked.length === 1 && checked[0].id === 'checkboxA') {
        //do something
    } 
    else {
        //do something else
    }
});
Russ Cam
How do you know 'checkboxA' will be the first element in the list?
a.feng
Russ Cam
Got it. Thanks!
a.feng
Awesome. thanks Russ!
Dirty Bird Design
I usually go with .size() to check the result set, and forgot that .length works too. Thanks!
AndrewDotHay
.size() just calls .length internally so I prefer to skip the extra function call and just check the property directly
Russ Cam
+1  A: 

What about selecting all checked checkboxes with

$('input:checkbox:checked')

And then running a check to make sure the length of this list is equal to 1?

a.feng