I have the following form:
echo '<form method="post" id="myForm" onsubmit="return formComp()">';
foreach($array as $value){
echo '<input type="checkbox" name="check[]" value="$value">';
}
echo '<input type="submit" name="ok" value="OK">';
echo '</form>';
and the javascript:
function formComp(){
var theForm=document.getElementById("myForm");
var a = theForm.elements['check[]'];
for( var i=1; i<a.length; i++){
if(!a[i].checked){
alert('Select an option');
return false;
}
else{
return true;
break;
}
}
}
I'm catching a php array,and use it in a loop so i can check each option if it meets the requirements(if it's checked).The problem is that it doesn't stop even if i check an option.When, atleast one option is checked,i need the loop to stop and return true,so it can be submited.
EDIT: By the way,the function returns true when i check every option.But i can't make it return true when atleast one is checked.
check[]is only a name ofHTMLelement. It is array, because it has[], as it is defined inHTMLspecification ツツツ But, anyway, check my answer, it should fit your needs.