I need to identify if any checkbox in a CheckBoxList is not checked (repeat NOT checked) client-side, so that I can change a “select All” option at the top to checked/unchecked using JQuery.
The other operations (to check/uncheck all and unchecking any options unchecking the “Select all” check) all work fine, but it would be nice to also have the “select all” reflect the state of all other checkboxes.
Assuming I already have var cbs = $('#myCheckBoxList :checkbox') what would I have to apply to it to find any checkbox where selected == false?
All the examples I have found are all about finding the checked state of a single checkbox, but I need to know if any is not checked.
tried cbs.find(':[checked="false"]') etc but I could not find a good example of find() filtering by attribute only.
Full code for the select-all toggle behavior I was writing
(including final solution derived from Chandu’s answer below):
$(window).load(function () {
var cbs = $('.myCheckBoxList :checkbox');
cbs.eq(0).click(function () {
var toggle = this.checked;
cbs.attr('checked', toggle);
});
cbs.slice(1).click(function () {
if (!this.checked) {
cbs.eq(0).attr('checked', false);
} else {
cbs.eq(0).attr('checked', cbs.slice(1).filter(':not(:checked)').length == 0);
}
});
});
You need to use the
:notselector along with:checkedTry this:
cbs.find(‘:not(:checked)’)or