1
<html>
<head>
<title> Algebra Reviewer </title>
<style type="text/css">
h2
{
color: white;
font-family: verdana;
text-shadow: black 0.1em 0.1em 0.2em;
text-align: center;
}
table
{
font-family:verdana;
color: white;
text-shadow: black 0.1em 0.1em 0.2em;
}
</style>
<script type="text/javascript">
function question1()
{
if ("quiz.question1.value=='d'")
{
alert ("That's the correct answer!");
} 
else
{
alert ("Oops! try again!");
}
}
</script>
</head>
<body>
</br>
<h2>
Here are 10 items for you to answer. You might need scratch paper- so get one before taking this reviewer.
</h2>
</br>
</br>
<center>
<table border="0" bgcolor="tan">
<tr> <td>
<ol>
<li> What is the equation of the line passing through point (3,8) and parallel to the line x - 3y = 5 ? </li>
</br>
</br> 
<input type = "radio" name = "question1" onclick = "quiz.question1.value=='a'"/> y = 1/3 x + 5
</br>
<input type = "radio" name = "question1" onclick = "quiz.question1.value=='b'"/> y = 3x + 7
</br>
<input type = "radio" name = "question1" onclick = "quiz.question1.value=='c'"/> y = 3/5 x + 3
</br>
<input type = "radio" name = "question1" onclick = "quiz.question1.value=='d'"/> y = 1/3 x + 7
</br>
</br>
<input type = "button" onclick = "question1()" name = "question1" value = "Submit"/>
<br/>
<br/>
</td> </tr> </table> </center>
</body>
</html

The answer here should be the 4th radio button, but I think that the if / else statement is wrong.

3
  • 1
    Use a validator and stop using tables for laout. Commented Mar 19, 2014 at 12:25
  • 1
    Please learn to love labels Commented Mar 19, 2014 at 12:25
  • 4
    Totally unrelated, but I think the server should validate the correct answer instead of javascript. If you don't do that your students will easily cheat looking in the source html code for the answer. Commented Mar 19, 2014 at 12:46

6 Answers 6

2

First of all, set the radios value attribute as the options. modify the following in your html

<input type = "radio" name = "question1" value='a'/> y = 1/3 x + 5
</br>
<input type = "radio" name = "question1" value='b'/> y = 3x + 7
</br>
<input type = "radio" name = "question1" value='c'/> y = 3/5 x + 3
</br>
<input type = "radio" name = "question1" value='d'/> y = 1/3 x + 7

and change the function as follows:

function question1()
{
  var selectedAns;
  var questions = document.getElementsByName("question1");
  for(var i = 0; i < questions.length; i++) {
     if(questions[i].checked == true) {
     selectedAns = questions[i].value;
     break;
  }

  if(selectedAns== 'd')
    alert ("That's the correct answer!");
  } 
  else
  {
   alert ("Oops! try again!");
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You never defined the quiz variable. Use value attribute to get selected radio button. Use the following code.

<html>
<head>
<title> Algebra Reviewer </title>
<style type="text/css">
h2 {
   color: white;
   font-family: verdana;
   text-shadow: black 0.1em 0.1em 0.2em;
   text-align: center;
}
table {
   font-family:verdana;
   color: white;
   text-shadow: black 0.1em 0.1em 0.2em;
}
</style>
<script type="text/javascript">
    function question1() {
       var option = document.querySelector('input[name = "question1"]:checked').value;
       if (option == 'd') {
          alert("That's the correct answer!");
       } 
       else {
          alert ("Oops! try again!");
       }
    }
</script>
</head>
<body>
</br>
<h2>
Here are 10 items for you to answer. You might need scratch paper- so get one before taking this reviewer.
</h2>
</br>
</br>
<center>
<table border="0" bgcolor="tan">
   <tr> 
     <td>
        <ol>
           <li> What is the equation of the line passing through point (3,8) and parallel to the line x - 3y = 5 ? </li>
           </br>
           </br> 
           <input type = "radio" name = "question1" value="a" /> y = 1/3 x + 5
           </br>
           <input type = "radio" name = "question1" value="b" /> y = 3x + 7
           </br>
           <input type = "radio" name = "question1" value="c" /> y = 3/5 x + 3
           </br>
           <input type = "radio" name = "question1" value="d" /> y = 1/3 x + 7
           </br>
           </br>
           <input type = "button" onclick = "question1()" name = "question1" value = "Submit"/>
           <br/>
           <br/>
        </td>
     </tr>
 </table>
</center>
</body>
</html>

1 Comment

+1 for this clean solution. But beware. this does not work in IE7 caniuse.com/queryselector (though, IE7 is used in <1% worldwide theie7countdown.com/educate-others)
0
  1. You never define quiz so when you attempt to assign a value to quiz.question1.value, the code throws an error
  2. Your if statement is testing to see if a string is a true value, which it always will be. You have too many quotes.

Comments

0

The name attribute for radio buttons and the submit button should not be same I guess. Try changing name attribute of submit button.

Comments

0
  1. For a first thing, you are missing a form element

    < form name="quiz" > ... < /form >

This needs to be present for your JavaScript code to work.

  1. name attributes for radio buttons and the submit button need to be different.

Comments

0

Ok, I fixed your issue by using a hidden field. Good effort for a beginner. But, this is not the best way to do it. Anyway, keep up the good work. You'll get there.

<html>
    <head>
    <title> Algebra Reviewer </title>
    <style type="text/css">
    h2
    {
    color: white;
    font-family: verdana;
    text-shadow: black 0.1em 0.1em 0.2em;
    text-align: center;
    }
    table
    {
    font-family:verdana;
    color: white;
    text-shadow: black 0.1em 0.1em 0.2em;
    }
    </style>
    <script type="text/javascript">

    function question1()
    {
    if (document.getElementById('question1Answer').value =='d')
    {
    alert ("That's the correct answer!");
    } 
    else
    {
    alert ("Oops! try again!");
    document.getElementById('question1Answer').value =''
    }
    }
    </script>
    </head>
    <body>
    </br>
    <h2>
    Here are 10 items for you to answer. You might need scratch paper- so get one before taking this reviewer.
    </h2>
    </br>
    </br>
    <center>
    <table border="0" bgcolor="tan">
    <tr> <td>
    <ol>
    <li> What is the equation of the line passing through point (3,8) and parallel to the line x - 3y = 5 ? </li>
    </br>
    </br> 
    <input type = "radio" name = "question1" onclick = "document.getElementById('question1Answer').value ='a'"/> y = 1/3 x + 5
    </br>
    <input type = "radio" name = "question1" onclick = "document.getElementById('question1Answer').value ='b'"/> y = 3x + 7
    </br>
    <input type = "radio" name = "question1" onclick = "document.getElementById('question1Answer').value ='c'"/> y = 3/5 x + 3
    </br>
    <input type = "radio" name = "question1" onclick = "document.getElementById('question1Answer').value ='d'"/> y = 1/3 x + 7
    </br>
    </br>
    <input type = "hidden" id = "question1Answer" value = ""/>
    <input type = "button" onclick = "question1()" name = "question1" value = "Submit"/>
    <br/>
    <br/>
    </td> </tr> </table> </center>
    </body>
    </html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.