How do I check if multiple checkboxes are unchecked in jQuery?

I have 3 checkboxes I need a check to see if all 3 of them are unchecked, but so far my effort to get this to work has not been successfully.

What is the correct way to do this in jQuery?

Some of my tries so far:

if( $("#adviceonprivatemail").not(":checked") && $("#adviceonmobile").not(":checked") && $("#adviceoncompanymail").not(":checked")  ){
  console.log('All is disabled')
}else{
  console.log('All is enable')
}    

if( $('#adviceonprivatemail').not(':checked').length) && $('#adviceonmobile').not(':checked').length) && $('#adviceoncompanymail').not(':checked').length) ){
  console.log('Alle er disabled')
} 

if( $('#adviceonprivatemail').not(':checked').length) && $('#adviceonmobile').not(':checked').length) && $('#adviceoncompanymail').not(':checked').length) ){
  console.log('Alle er disabled')
} 

     

>Solution :

Use prop

  if( $("#adviceonprivatemail").prop("checked") && $("#adviceonmobile").prop("checked") && $("#adviceoncompanymail").prop("checked")  ){
      alert('All are enabled')
    }else{
      alert('Some are disabled')
    }    

Also you can do something like: If you use some new "data-attribute" with the same identificator, you can select them 3 at once, and then do a foreach until someone is checked

<input type="checkbox" data-chk="this" id="adviceonprivatemail" />
<input type="checkbox" data-chk="this" id="adviceonmobile" />
<input type="checkbox" data-chk="this" id="adviceoncompanymail" />

if( $("[data-chk='this']").prop("checked") == false ){
  alert('All are disabled')
}else{
  alert('Some are enabled')
}  

Leave a Reply