Solved

Evaluating a CheckBox Form

Posted on 2011-09-07
3
164 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

707 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

17 Experts available now in Live!

Get 1:1 Help Now