Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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')
}  
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading