Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Evaluating a CheckBox Form

Posted on 2011-09-07
3
Medium Priority
?
203 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

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?
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…

704 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