Solved

Evaluating a CheckBox Form

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add a Location and Color Price based on select box selected option 17 65
need help with share buttons 11 65
href return to normal window 5 28
Jquery 7 28
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

679 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