(page requirements)

Creating questions and answers forms for your web pages

So you want to quiz your visitors? Following JavaScript code can help you to easily create Q and A forms for your web pages without using CGI scripts.
 
The following code will let you display a dialog box indicating whether the selected answer was correct or not. You can also make it redirect the visitor to another web page depending on the answer.
 
1. First, cut-and-paste the following JavaScript code on to your web page (closer to the top of the page right below the BODY tag is a good location):
 
<SCRIPT language="JavaScript"
        type="text/javascript">
<!--
//
// Documentation:
//   http://chami.com/tips/javascript/
//
function checkAnswer(quizForm,
                     theAnswer,
                     urlRight,
                     urlWrong)
{
  var s = "?";

  // go through the "current choices"
  // to find the selected choice.
  // radio boxes pointing to choices
  // must be named "cc"
  // change if necessary
  //
  var i = 0;
  for(;i<quizForm.elements.length;i++)
  {
    if(("cc" ==
        quizForm.elements[i].name) &&
       (quizForm.elements[i].checked))
    {
      s = quizForm.elements[i].value;
    }
  }

  // no choice was selected
  //
  if("?" == s)
  {
    alert("Please make a selection.");
    return false;
  }

  // check if we have the correct
  // choice selected
  //
  if(s == theAnswer)
  {
    alert("'"+s+"' is correct!");
    if(urlRight)
    {
    document.location.href = urlRight;
    }
  }
  else
  {
    alert("'"+s+"' is incorrect.");
    if( urlWrong )
    {
    document.location.href = urlWrong;
    }
  }

  // return "false" to indicate not to
  // submit the form.
  // change this to "true" if the form
  // "action" is valid,
  // i.e. points to a valid CGI script
  //
  return false;
}
//-->
</SCRIPT>
Listing #1 : JavaScript code. Download chkanswr.htm (0.72 KB).
 
2. Insert the following HTML tags to create your quiz form (sample code shown, modify as necessary):
 
What is JavaScript?

<FORM method="POST"
      onSubmit="return checkAnswer(this,'B');"
>

<INPUT TYPE="RADIO" VALUE="A" NAME="cc">
A. Another name for Java<BR>

<INPUT TYPE="RADIO" VALUE="B" NAME="cc">
B. A scripting language mostly for the web<BR>

<INPUT TYPE="RADIO" VALUE="C" NAME="cc">
C. When you use Java without compiling<BR>

<INPUT TYPE="SUBMIT" VALUE="Submit Answer">

</FORM>
Listing #2 : HTML code. Download quiz1.htm (0.37 KB).
 
Demo:
 
What is JavaScript?
A. Another name for Java
B. A scripting language mostly for the web
C. When you use Java without compiling

 
That's it! Simply keep changing the questions and answers to create more quiz forms.
 
Just be sure to setup the HTML tags in the following format. The NAME tag must be "cc" and there should not be any other fields named "cc". The letter specified for the VALUE tag is the ID for the answer (start with "A" and keep incrementing as in the above sample. "X" is the answer ID in the following sample code):
 
<INPUT TYPE="RADIO" VALUE="X" NAME="cc">
Custom answer text
 
The other thing to check is the onSubmit tag for the quiz form as shown below. "Y" is the letter ID for the correct answer. This letter must be assigned to one of the answers in the quiz form.
 
<FORM method="POST"
onSubmit="return checkAnswer(this,'Y');"
>
 
Let's look at another sample quiz form. This one will redirect the visitor to correct.htm if the answer is correct or to wrong.htm otherwise. Note the two additional parameters specified in the "checkAnswer()" function:
 
What is JavaScript?

<FORM method="POST"
      onSubmit=
      "return checkAnswer(this, 'C', 
      'correct.htm', 'wrong.htm');"
>

<INPUT TYPE="RADIO" VALUE="A" NAME="cc">
Another name for Java<BR>

<INPUT TYPE="RADIO" VALUE="B" NAME="cc">
When you use Java without compiling<BR>

<INPUT TYPE="RADIO" VALUE="C" NAME="cc">
A scripting language mostly for the web<BR>

<INPUT TYPE="SUBMIT" VALUE="Submit Answer">

</FORM>
Listing #3 : HTML code. Download quiz2.htm (0.39 KB).
 
Now that you know how to create a quiz form using JavaScript, want to make it a pop-quiz form? This page can help you to make your quiz forms pop-up.
 
 
Applicable Keywords : HTML, Internet Explorer, Explorer 3.x, Explorer 4.x, Explorer 5.x, Internet, JavaScript, JavaScript 1.0, JavaScript 1.1, JavaScript 1.2, Netscape Navigator, Navigator 2.x, Navigator 3.x, Communicator 4.x, Netscape 6.x, World Wide Web
 
 
 
Copyright © 2009 Chami.com. All Rights Reserved. | Advertise | Created in HTML Kit editor