Running Total with Multiple Radio Button Options
A am trying to run a tally of questions results from mulitple (25)radio option form fields using if/else if. Seems to work, but does not compute correctly.
Probably made a stupid mistake as usual. Pleas respond to my email address at: [email protected]
Thanks -->Rick
============= function scorequiz(form) {
tally=0
if(form.Q1.value!=null && form.Q1.value=="1") {tally=tally+1};
else if(form.Q1.value!=null && form.Q1.value=="2") {tally=tally+2};
else if(form.Q1.value!=null && form.Q1.value=="3") {tally=tally+3}
else if(form.Q1.value!=null && form.Q1.value=="4") {tally=tally+4};
if(form.Q2.value!=null && form.Q2.value=="1") {tally=tally+1};
else if(form.Q2.value!=null && form.Q2.value=="2") {tally=tally+2};
else if(form.Q2.value!=null && form.Q2.value=="3") {tally=tally+3};
else if(form.Q2.value!=null && form.Q2.value=="4") {tally=tally+4};
if(form.Q3.value!=null && form.Q3.value=="1") {tally=tally+1}
else if(form.Q3.value!=null && form.Q3.value=="2") {tally=tally+2}
else if(form.Q3.value!=null && form.Q3.value=="3") {tally=tally+3}
else if(form.Q3.value!=null && form.Q3.value=="4") {tally=tally+4}
<snip> Etc. to 25, then...
form.SCORE.value =eval(tally)
}
Radio Buttons look like this:
<INPUT TYPE="radio" NAME="Q1" value="1" onclick=Q1.value="1">Strongly Disagree<br>
<INPUT TYPE="radio" NAME="Q1" value="2" onclick=Q1.value="2">Disagree<br>
<INPUT TYPE="radio" NAME="Q1" value="3" onclick=Q1.value="3">Agree<br>
<INPUT TYPE="radio" NAME="Q1" value="4" onclick=Q1.value="4">Strongly Agree</font>
</
Command button into a field box:
<INPUT TYPE="button" NAME="submit" VALUE="Compute Quiz tally" onclick=scorequiz(this.form)>
Vincent Puglia posted this at 02:28 — 1st February 2000.
They have: 634 posts
Joined: Dec 1999
Hi Rick,
Didn't cut & paste your code to see what's wrong -- mostly because you might want to see the "Tallying Radio Buttons" script at my site, but also because I try to stay away from 25 if/else statements -- bound to create some sort of havoc. My scripts (there are 2 onsite & 1 offsite) pass objects and/or values so that the actual checking & tallying code is short.
The code is modifiable & comes with explanations.
Vinny GrassBlade: cut&paste javascript
Where the world once stood
the blades of grass cut me still
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.