I am a beginner at Javascript and I am creating a form. The thing is I have a function that returns the value of the checkboxes (to check if they were checked or not) when the user submits his or her answer. I want to give the user a message if he / she does not select a value in the checkbox (not an error message). I have tried the following Javascript code:
function loopForm() {
var cbResults = " ";
var error = " You did not select a value";
for (var i = 0; i < myForm.elements.length; i++) {
if (myForm.elements[i].type == "checkbox") {
if (myForm.elements[i].checked == true) {
cbResults += myForm.elements[i].value + "<br />";
}
}
}
if (cbResults != undefined) {
return cbResults;
} else {
return error;
}
}
But when I submitted the results without checking the checkbox (once again this is not an error message) the message was not shown. (I stored the message in the error var). Can anyone help me solve this problem ?? Any help would be greatly appreciated. Please note that this is not my Full Code but just a portion of it.
Aucun commentaire:
Enregistrer un commentaire