I have multiple checkboxes and I would like to know if one of them is checked. My code does not give me any error, but I don't get the results I am expecting. When I run the code always get the first alert which is inside the for loop. The following is my code:
function validate(){
if(document.getElementById('postTW').checked || document.getElementById('postFB') ){
var checker = document.getElementsByName('twitter[]');
for (var i = 0; i < checker.length; i++) {
if(checker[i].checked){
return true;
}
else{
alert("Not enough arguments ");
return false;
}
};
}
else{
alert("Not enough arguments to submit the form");
return false;
}
}
My form is method = post and looks like the following
<input class="twGroup" type="checkbox" name="twitter[]" value="x"/>
<input class="twGroup" type="checkbox" name="twitter[]" value="y"/>
<input class="twGroup" type="checkbox" name="twitter[]" value="z"/>