Solved

Evaluating a CheckBox Form

Posted on 2011-09-07
3
168 Views
Last Modified: 2012-05-12
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.

Thanks

RG
<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>
</tr>
            
 <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);">
</form>

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

}

Open in new window

0
Comment
Question by:rgarimella
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 36495546
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;
            break;
        }
    }
    if (incorrect) {
        alert ("Incorrect");
    } else{
        alert("correct");
    }
}

Open in new window

0
 
LVL 29

Expert Comment

by:Badotz
ID: 36495883
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
0
 

Author Closing Comment

by:rgarimella
ID: 36495986
Great Thx.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now