Checkboxes and 'submit' button issues

  You are currently not logged in. You can view the forums, but cannot post messages. Log In | Register

12-Jun-17 12:44
I am designing a quiz with checkboxes for selection, two being the correct answers, the others not. I have managed to have a message appear if either the two correct options are selected or if the wrong options are selected. My problem is simply clicking my 'check my answer' button with out selecting any checkbox results in the answer being shown. A message appears in either correct or incorrect options are selected stating the answer. I don't want users to see the answer unless they have selected checkboxes.

I have tried the following code:



function quiz4() {
var a4 = document.getElementsByName('q4');
var a4answers = new Array();
var a4right = new Array('a','c');
var a4bool = true;

for(i = 0; i < a4.length; i++) {
if(a4[i].checked) {
a4answers.push(a4[i].value);
}
}

a4answers.sort();
a4right.sort();

if(a4answers.length == a4right.length) {
for(i = 0; i < a4answers.length; i++) {
if(a4answers[i] != a4right[i]) {
a4bool = false;
break;
}//end of if loop
}//end if for loop
} else {
a4bool = false;
}

if(!document.getElementsByName('q4').checked) {
document.getElementById('oCorrectAnswer4').innerHTML = "Please select A), B), C) or D).";
}

This fails to stop the answer being shown, despite the fact that I used an if statement to request users to select a checkbox. I can only assume it's the wrong type of if statement, or in the wrong place, or is incomplete.

I would appreciate any help in solving the issue of having checkboxes checked to see if selected, and if so if the correct ones are selected.

Thanks.
if(a4bool == true) {
correctAnswers++;
document.getElementById('oCorrectAnswer4').innerHTML = "Correct: The answers are A) Keyboard and C) Fingerprint Scanner.";
}//end if
else {
document.getElementById('oCorrectAnswer4').innerHTML = "Incorrect: The answers are A) Keyboard and C) Fingerprint Scanner.";
}// end else

var inputs = document.getElementsByName('q4');
for(var i = 0; i < inputs.length; i++) {
if(inputs[i].type == 'checkbox') {
inputs[i].disabled = true;
}
}
}//end of q4


--
Stephen Berry
12-Jun-17 21:25
Start with the submit button disabled and only enable it if the correct boxes are checked.

However the issue is moot, as it is in javascript and the answers can be seen by simply viewing the source code.

--
Chris.
So long, and thanks for all the fish.
http://webmaster-talk.eu/
13-Jun-17 07:02
Thanks for the help.

--
Stephen Berry

 
New posts
Old posts

Follow Elated