Evaluating a CheckBox Form

Hi Folks,

I want to evaluate Multiple Answer Form. The kicker is, I CANNOT use any variables in the HEAD section of the HTML Page. (Dont ask me why, but that is what it is.)

I have the form below  and the correct answer is in the hidden field called CorrectChoice where 1 is for Check Box Selected and 0 is for Check Box NOT selected.

In this case the correct answers are 1st and the Last Check Box.

Any help is appreciated.


<form name="mcForm">
<input type="hidden" name="CorrectChoice" value="1,0,0,1">

<input type="checkbox" name="checkbox1" value="1" accesskey="A"  id="1">
<font size="2" face="Arial, Helvetica, sans-serif" class="text1">A.</font>
<font size="2" face="Arial, Helvetica, sans-serif" class="text1"><label for="1">Microsoft&reg; Internet Explorer® 7 or later</label></font>
 <input type="checkbox" name="checkbox2" value="2" accesskey="B"  id="2" >
<font size="2" face="Arial, Helvetica, sans-serif" class="text1">B.</font></td>
<font size="2" face="Arial, Helvetica, sans-serif" class="text1"><label for="2">Adobe® Flash® Player 9 or later</label></font>
 <input type="checkbox" name="checkbox3" value="3" accesskey="C"  id="3" >
<font size="2" face="Arial, Helvetica, sans-serif" class="text1">C.</font></td>
<font size="2" face="Arial, Helvetica, sans-serif" class="text1"><label for="3">Adobe® Reader</label></font>

<input type="checkbox" name="checkbox4" value="4" accesskey="D"  id="4" >
<font size="2" face="Arial, Helvetica, sans-serif" class="text1">D.</font></td>
<font size="2" face="Arial, Helvetica, sans-serif" class="text1"><label for="4">Microsoft® Word 97 or later</label></font>
<input type="button" value="Submit" onClick="checkAnswerMa(mcForm);">

function checkAnswerMa(mcForm){
//Code here
If (user selected 1st and the Last Check box) {
alert ("Incorrect");


Open in new window

Who is Participating?
leakim971Connect With a Mentor PluritechnicianCommented:
Check this page : http://jsfiddle.net/9t85w/

function checkAnswerMa(mcForm){
    var incorrect = false;
    var answers = document.getElementsByName("CorrectChoice")[0].value.split(",");
    for(var i=1;i<=answers.length;i++) {
        var checkbox_i = document.getElementsByName("checkbox"+i)[0];
        var checkbox_i_checked = checkbox_i.checked;
        if((checkbox_i_checked && answers[i-1] == "0") || (!checkbox_i_checked && answers[i-1] == "1")) {
            incorrect = true;
    if (incorrect) {
        alert ("Incorrect");
    } else{

Open in new window

Your ID attributes are invalid. This from http://www.w3schools.com/tags/att_standard_id.asp

Attribute Values

Value   Description
id      Specifies a unique id for an element.

Naming rules:

 o   Must begin with a letter A-Z or a-z
 o   Can be followed by: letters (A-Za-z), digits (0-9), hyphens ("-"), underscores ("_"), colons (":"), and periods (".")
 o   Values are case-sensitive
rgarimellaAuthor Commented:
Great Thx.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.