Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onClick Multiple Choice Quiz making null memory 1

Status
Not open for further replies.

jaslr

Programmer
Aug 16, 2001
67
AU
I am using the following script (simplified with most of the html taken out).

If someone is to press the "Check Score" button more than 5 times the value in the SCORE text form object is reset to 0. Obviously this is just a sample test and the real test is actually 15 questions. If someone goes through the test and even gets 15/15 correct they still get '0'.

I am thinking that by having onClick=Q2.value="" it is somehow wiping out the memory?

If so, is there another way to set a variable - other than onclick - that can be used with this scorequiz() function?



____

Code:
<head>
<script language="javascript">
<!-- Begin 
function scorequiz(form) {

tally=0
 
if(form.Q1.value!=null && form.Q1.value=="*") {tally=tally+1}
if(form.Q2.value!=null && form.Q2.value=="*") {tally=tally+1}

 form.SCORE.value =eval(tally);
}

}

</script>
</head>



_______________________


<body>
 <p> 2. A Cat has how many legs?
<input type="radio" name="Q1" VALUE=" * 4" onClick=Q2.value="*">

<input type="radio" name="Q1" VALUE="1" onClick=Q2.value="B">

<input type="radio" name="Q1" VALUE="8" onClick=Q2.value="C">

<input type="radio" name="Q1" VALUE="6" onClick=Q2.value="D">

Birds have: 
<input type="radio" name="Q2" VALUE=" * Wings" onClick=Q3.value="*">
 <input type="radio" name="Q2" VALUE="Horns" onClick=Q3.value="B">
 <input type="radio" name="Q2" VALUE="Flippers" onClick=Q3.value="C">
 <input type="radio" name="Q2" VALUE="Nippers" onClick=Q3.value="D">


<INPUT  NAME="SCORE" TYPE="text" maxlength="2" size="5" READONLY>

<input type="button" value="Score Quiz" onClick=scorequiz(this.form) name="button">
             
[/code
 
I should add for example if I simply select 1 radio button that I KNOW is correct and press "check answers" I get 1 / 15.

However, if I keep clicking that button about 8 times it is reset to 0. Now, clicking it 8 times wouldn't be a problem if the quiz was very small (like the above example) but when those 8 clicks are actually the user selecting other answers then it's reset to zero.

I hope I am making sense here.
 
Editing your code slightly, I can't seem to reproduce your results:

Code:
<html>
<head>
<script language="javascript">
<!-- Begin 
function scorequiz(form) 
{

tally=0;
 
if (form.A1.value=="*") 
{
  tally++;
}

if (form.A2.value=="*") 
{
  tally++;
}

form.SCORE.value = tally;

}

// end -->
</script>
</head>

<body>
<form name="aform">
 <p> 2. A Cat has how many legs?<br>
4<input type="radio" name="Q1" VALUE=" * 4" onClick=A1.value="*"><br>

1<input type="radio" name="Q1" VALUE="1" onClick=A1.value="B"><br>

8<input type="radio" name="Q1" VALUE="8" onClick=A1.value="C"><br>

6<input type="radio" name="Q1" VALUE="6" onClick=A1.value="D"><br>
<input type="hidden" name="A1" value="">

Birds have: 
Wings<input type="radio" name="Q2" VALUE=" * Wings" onClick=A2.value="*"><br>
Horns <input type="radio" name="Q2" VALUE="Horns" onClick=A2.value="B"><br>
Flippers <input type="radio" name="Q2" VALUE="Flippers" onClick=A2.value="C"><br>
Nippers <input type="radio" name="Q2" VALUE="Nippers" onClick=A2.value="D"><br>
<input type="hidden" name="A2" value="">

<INPUT NAME="SCORE" TYPE="text" maxlength="2" size="5" READONLY>

<input type="button" value="Score Quiz" onClick=scorequiz(this.form) name="button">
</form>
</body>
</html>

--Chessbot

"So it goes."
Kurt Vonnegut, Slaughterhouse Five
 
Ok, do you mean you can't get it working

OR

You arent' getting my problems and it's working fine?
 
Sorry, I posted before your second post. I didn't realize it didn't pertain to short quizzes.

I can get it working, and cannot reproduce your problem.

--Chessbot

"So it goes."
Kurt Vonnegut, Slaughterhouse Five
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top