well I am making a quiz. It uses radio buttons and a see my results button. I cant see anything wrong with the whole page but it doesent work...
Code:
<html>
<head>
<title></title>
<script>
var q1 = "0"
var q2 = "0"
function addup() {
if (document.quiz.q1.yes.checked) {
q1 + 1 = q1
}
}
results;
function results() {
q1 + q2 = results
alert("you got " + results + " points")
}
</script>
</head>
<body onload="addup()">
<link REL="StyleSheet" TYPE="text/css" HREF="style.css">
<table width=100% class=mainquiz><td width=100% class=main2quiz>
<h1>The weird quiz</h1>
This page will tell you about how weird you are.<br><br>
<h2>Directions:</h2>
Check the one that is closest to your answer.<br><br>
<form name=quiz>
Question 1:<br>
If you saw gum stuck on the bottom of a table would you eat it?<br>
<input type=radio name="q1" value="Yes">Yes<br>
<input type=radio name="q1" value="No">No<br>
<input type="button" value="See my results!!" onclick="results()">
</form>
<!--#include virtual="/bottom.html" -->
Couple things I see, you have a variable[results] named the same name as a your function.
Code:
function addup() {
if (document.quiz.q1.yes.checked) {
q1 + 1 = q1
}
}
results;
Are you meaning to call the results function there? Rename your variables accordingly, and this doesn't look right:
q1 + 1 = q1
I think you wanted:
q1++
Same with this:
q1 + q2 = results
If you test the page out in firefox it's really good with helping you debug your code.
The value of an element is not used in finding it in the document in the way you have written the code. If you want a variable to be a number, don't use quotes around the value, that makes it a string. If you set your variables globally, changing the radio buttons' states will not change the value in the way expected. There is no reason to run any function onload for this nor, to have two functions. The previous poster's comments were also correct, mostly. Try this one:
Code:
<html>
<head>
<title></title>
<script type="text/javascript">
function addup() {
var q1 = 0
var q2 = 0
if (document.forms.quiz.q1['yes1']){
if (document.forms.quiz.q1['yes1'].checked)
q1++
}
else if (document.getElementById){
if (document.getElementById('yes1').checked)
q1++
}
else
return;
var total = q1 + q2
alert("you got " + total+ " points")
}
</script>
</head>
<body>
<link REL="StyleSheet" TYPE="text/css" HREF="style.css">
<table width=100% class=mainquiz><td width=100% class=main2quiz>
<h1>The weird quiz</h1>
This page will tell you about how weird you are.<br><br>
<h2>Directions:</h2>
Check the one that is closest to your answer.<br><br>
<form name=quiz>
Question 1:<br>
If you saw gum stuck on the bottom of a table would you eat it?<br>
<input id="yes1" type=radio unchecked name="q1">Yes<br>
<input id="no1" type=radio unchecked name="q1">No<br>
<input type="button" value="See my results!!" onclick="addup()">
</form>
<!--#include virtual="/bottom.html" -->
Thanks! I got it to work but I can't add another question! i dont get it
and is there a way to assign different point values to yes, no? because I want to have like 4 different choices!
And in the end It won't be a alert box that comes up, I will write a function to check the result var and it will take you to a different page depending on what number you came up with. so like:
points 1-5 you go to page 1
points 6-10 you go to page 2
and on and on
Last edited by GO ILLINI; 07-05-2005 at 08:54 PM..
I don't mean to be disparaging however, given what seems to be your level of expertise in coding, I'm not surprised you are having problems expanding this script and its accompanying HTML markup. Tell you what, I'll make a copy of your post that I am responding to and paste it in as a comment to the demo file I made from my last post. When I get the chance I'll write a commented version showing how to include the features you've requested and post it, attach it or link it, back to this thread.
Ah well, it was just that there was so much more amiss with your original code than just radio buttons. I thought I was helping you out. Anyways, here is the code, as promised: