Javascript: Check if any checkbox is checked

If you want to check if any checkbox is selected, but checkboxes have all the same name and are so part of an array, here is an example of how to do:

anychecked = false;
for(var i=0; i< document.forms.form1["mycheckboxes"].length;i++) {
  if(document.forms.form1["mycheckboxes"][i].checked == true) {
    anychecked = true;
    break;
  }
}
if(anychecked)
  alert('At least one checkbox is checked');

Leave a Reply

Your email address will not be published. Required fields are marked *


*