Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    May 2005
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Writing a multiple choice quiz

    I want to write a javascript for a multiple choice quiz. If I can get the code for one question, the rest I may be able to do. I want to alert the user if they are wrong and give the correct answer. If the user get the question right I want the alert to say "you are correct". The last part is what I am having trouble with. This is what I have so far. Thanks for any hints you can give me.

    <html><head><title>Multiple choice quiz</title>
    <script language="JavaScript">
    function quiz(question1){
    if(question1.q1.value != "4"){
    alert("The correct answer is: 4");
    }
    else{
    if(question1.q1.value == "4");
    alert("You are correct!");
    }
    }
    </script>
    </head>
    <body>
    <b>
    <form name="question1"
    method="post"
    onSubmit="return quiz(document.question1)"><p>
    <font size="+1"><p>
    Question 1:<br><br>
    2 plus 2 is:<br>
    <input type=radio
    name="q1"
    value="45">a. 45<br>
    <input type=radio
    name="q1"
    value="4">b. 4<br>
    <input type=radio
    name="q1"
    value="3">c. 3<br>
    <input type=radio
    name="q1"
    value="6">d. 6<br>
    <p>
    <input type=button
    value="Click to Submit" onClick="quiz(this.form);">
    <input type="reset" value="Clear">
    </form>
    </body>
    </html>

  • #2
    New Coder
    Join Date
    Jun 2005
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <html><head><title>Multiple choice quiz</title>
    <script language="JavaScript">
    function quiz(question1){
    //alert(question1.q1[1].checked);
    if(question1.q1[1].checked == false){
    alert("The correct answer is: 4");
    }
    else{
    if(question1.q1[1].checked == true);
    alert("You are correct!");
    }
    }
    </script>
    </head>
    <body>
    <b>
    <form name="question1"
    method="post"
    onSubmit="return quiz(document.question1)"><p>
    <font size="+1"><p>
    Question 1:<br><br>
    2 plus 2 is:<br>
    <input type=radio
    name="q1"
    value="45">a. 45<br>
    <input type=radio
    name="q1"
    value="4">b. 4<br>
    <input type=radio
    name="q1"
    value="3">c. 3<br>
    <input type=radio
    name="q1"
    value="6">d. 6<br>
    <p>
    <input type=button
    value="Click to Submit" onClick="quiz(this.form);">
    <input type="reset" value="Clear">
    </form>
    </body>
    </html>
    Read the section on debugging, e.g. uncomment the above alert statement, FireFox seems to be more informative about the types of html/dom elements being accessed.

  • #3
    New Coder
    Join Date
    May 2005
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks

    Thanks devosc,

    That is what I was looking for.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •