You have syntax errors in your 2nd and 3rd tries (you have an extra )
after each .length
). It should be $('#adviceonprivatemail').not(':checked').length
not $('#adviceonprivatemail').not(':checked').length)
.
Additionally, :not
is kind of a selector so it returns any matching elements, while :is
is a boolean check, which returns true/false. Which is why $("#adviceonprivatemail").not(":checked")
doesn't work, but $("#adviceonprivatemail").not(":checked").length
does (0 is falsey, 1+ is truthy). To replicate the .not
behavior, you can do !$("#adviceonprivatemail").is(":checked")
which is the logical way of saying "if it's not checked".
And per the linked question, using .prop('checked')
is another way to check this. But again, you'll need to invert the logic using !
like in the first example below.
$('input').on('change', function() {
// https://stackoverflow.com/a/6438222/1499877
// use `.is(':checked')`
if (!$("#adviceonprivatemail").is(":checked") &&
!$("#adviceonmobile").is(":checked") &&
!$("#adviceoncompanymail").is(":checked")
) {
console.log('All are disabled 1');
} else {
console.log('Some are enabled 1');
}
// use `.not(':checked').length`
if (
$('#adviceonprivatemail').not(':checked').length &&
$('#adviceonmobile').not(':checked').length &&
$('#adviceoncompanymail').not(':checked').length
) {
console.log('All are disabled 2');
} else {
console.log('Some are enabled 2');
}
// https://stackoverflow.com/a/6458172/1499877
// use `.prop('checked')`
if (!$("#adviceonprivatemail").prop('checked') &&
!$("#adviceonmobile").prop('checked') &&
!$("#adviceoncompanymail").prop('checked')
) {
console.log('All are disabled 3');
} else {
console.log('Some are enabled 3');
}
});
$('input').first().change();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" id="adviceonprivatemail" />
<input type="checkbox" id="adviceonmobile" />
<input type="checkbox" id="adviceoncompanymail" />