SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    USA
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Passing values using javascript

    I am trying to create an interactive quiz using javascript but I can't quite get it to pass the values properly. Following is a sample of how I have my code , it is giving always the total score to be zero instead of checking the right answers. How can I pass the checked value and check it and increment the score. Thanks for any help.

    Code:
    <html><head>
    
    function checkAnswers(form) 
    {
    
      if(form.Choice1.value == "Fish") 
       {
    alert "The value is " &form.Choice1.value;
         form.myScore1.value = eval(1);}
      else 
       {
         form.myScore1.value = eval(0);
       };
    
      if(form.Choice2.value == "Asia") 
       {
         form.myScore2.value = eval(1);}
      else 
       {
         form.myScore2.value = eval(0);
       };
    
      if(form.Choice3.value == "China") 
       {
         form.myScore3.value = eval(1);}
      else 
       {
         form.myScore3.value = eval(0);
       };
    }
    
    function sumScores(form) 
    {
    
      form.totScore.value  = eval(form.myScore1.value)
      + eval(form.myScore2.value)
      + eval(form.myScore3.value);
    }
    
    </script>
    </head>
    
    <body>
    
    <table border = "1">
    <tr>
    <td>
    1.&nbsp;&nbsp;Which one is not a mammal
    </td>
    <td><input TYPE="TEXT" NAME="myScore1" SIZE="3"></td>
    </tr>
    <tr>
    <td><input type="radio" name="Choice1" onClick="value=Elephant">Elephant
    <input type="radio" name="Choice1" onClick="value=Fish">Fish
    <input type="radio" name="Choice1" onClick="value=Dog">Dog
    <input type="radio" name="Choice1" onClick="value=Cat">Cat
    </td></tr>
    
    <tr>
    <td>
    2.&nbsp;&nbsp;The biggest continent
    </td>
    <td><input TYPE="TEXT" NAME="myScore2" SIZE="3"></td>
    </tr>
    <tr>
    <td><input type="radio" name="Choice2" onClick="value=Asia">Asia
    <input type="radio" name="Choice2" onClick="value=Australia">Australia
    <input type="radio" name="Choice2" onClick="value=Africa">Africa
    <input type="radio" name="Choice2" onClick="value=North America">North America
    </td></tr>
    
    
    <tr>
    <td>
    3.&nbsp;&nbsp;The country with the highest population
    </td>
    <td><input TYPE="TEXT" NAME="myScore3" SIZE="3"></td>
    </tr>
    <tr>
    <td><input type="radio" name="Choice3" onClick="value=America">America
    <input type="radio" name="Choice3" onClick="value=India">India
    <input type="radio" name="Choice3" onClick="value=Australia">Australia
    <input type="radio" name="Choice3" onClick="value=China">China
    </td></tr>
    
    <tr>
    <input type="button" value="Check Answers" onClick="checkAnswers(form);
    sumScores(form);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="reset" value="Clear form" name="STARTOVER"></center>
    
    </tr>
         
    <center>
    <tr>
    <font SIZE="+1"><font COLOR="#6600FF">Your total number of points is</font></font>&nbsp;
    <font COLOR="#000000">&nbsp;<input TYPE="TEXT" NAME="totScore" SIZE="5"></font>&nbsp;
    out of 10.</center>
    </tr>
    </table>
    </body>
    </html>
    mayflower

  2. #2
    SitePoint Addict MadDog's Avatar
    Join Date
    Aug 2002
    Location
    Oregon, USA
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Im not sure what your after here, are you trying to increment the "myScore3" value?
    Drew Gauderman
    ASP/MSSQL/AJAX-Javascript/HTML/CSS Coder
    iPortalX The most secure FREE ASP portal!

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    USA
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If the answer is right then I increment the corresponding myScore from 0 to 1 and then in the sumScore I am adding myScore1 through myScore3 to get the total score. But apparently it is not passing the selected radio button value properly because each of the answer even if it is right it shows as 0.


Bookmarks

Posting Permissions

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