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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

scoring a form with radio buttons

Status
Not open for further replies.

tackaman

IS-IT--Management
May 24, 2011
5
US
I have a form that has 20 questions and each question has 5 answers (each being a radio button). As the user answers each question by clicking the appropriate radio button, I call a function that scores the answer. The function is below and it works well.

The problem is that now i need to add additioal fields to this form. Some of those new fields are also radio button groups. When I add these, it breaks my function below. I have tried to place the questions that I want to score in a <div> but I can't figure out the code.

Original Function:
Code:
Function ScoreMe()

document.GetElementByID("TotScore").value = 0

For each el in document.forms(0).elements
	If el.type = "radio" and el.checked = true then
		document.GetElementByID("TotScore").value = CInt(document.GetElementByID("TotScore").value) + CInt(el.value)
	End If
Next

End Function
 
The function looks OK. What about your HTML code? It's likely there problem is there.

You may want to also post this thread in the HTML, XHTM, CSS forum ( along with the HTML code.

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
This function is working when I have only radio button groups that I want to include in my calculation. When I add another radio button group to the form that's not supposed to be scored, that's when I get an error (because the value of that new group is text and can't be converted to an int and the function loops through all radio buttons on the form).

I tried to change the function to loop through just radio elements in my <div> but I can't figure out the sytax.
 
I see.

You could check to see if the value is numeric before scoring.
Code:
Function ScoreMe()

document.GetElementByID("TotScore").value = 0

For each el in document.forms(0).elements
    If el.type = "radio" and el.checked = true [green]and isNumeric(el.value)[/green] then
        document.GetElementByID("TotScore").value = CInt(document.GetElementByID("TotScore").value) + CInt(el.value)
    End If
Next

-Geates

End Function

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Thanks. That works. Would it also be possible to assign a class to all of the radio buttons to include and use it instead of looking for an int? I could see a scenario where I might have two separate sections on a form where I have to calculate different scores, so that wouldn't work in this case.

I tried to add

and el.class = "ScoreIt"

but that didn't work
 
.name might

<input type="radio" [green]name="ScoreIt"[/green]>

Code:
If el.type = "radio" and el.checked = true and [green]el.name = "ScoreIt"[/green] then

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top