PDA

View Full Version : trouble looping through form name



dajohnson1s
Sep 16th, 2007, 07:33 AM
My current situation is developing a True/False style of quiz.

This is my function:
<head>
<script type="text/javascript">

function vote()
{
var t="Your favourite sport is: "
var result = 0;
var counter = 0;

while(counter < 4){
for (i=0; i<document.myform.sports.length; i++)
{
if (document.myform.sports[i].checked==true)
{
t =t +document.myform.sports[i].value
result = result + document.myform.sports[i].value

}
}
counter++;
}
</script>

The body: (This is only one of about 12 questions along with the vote button)


<tr>
<td height="30" align="center" bgcolor="#EEEEEE">
<input type="radio" name="c" value="1"></td>
<td align="center" bgcolor="#EEEEEE">
<input type="radio" name="c" value="0"></td>
<td align="left" class="main" bgcolor="#EEEEEE">Do unexpected expenses cause a serious strain on your finances?</td>
</tr>
<input type="button" value="VOTE" onClick="vote()" /><br>

The only way that I know how to do what I am desiring is to put all the questions in a table (3 columns) with a true/ false radio button off to the side followed by the question.

With the above function, excluding the while loop, I am able to read the value of the true false question. What I am wanting to accomplish is to loop through each of the questions, determine the value and add this to another variable and if the total is over a set value, in my case 7, i output a text message via a text box.

I hope this is clear. If anymore code or information is needed I would be greatful to provide whatever is needed.

Thanks in advance.

nikkiH
Sep 17th, 2007, 03:55 PM
I'm not totally sure what you're asking.
Is it that you have 12 radio button groups, each with a true / false, and you want to determine how many of those 12 have a true?

dajohnson1s
Sep 17th, 2007, 09:40 PM
yes, that is exactly what I am wishing to do.

I am just unsure of how to go about this process. If you would like to see the code I have so far I would be more than happy to provide this.

nikkiH
Sep 17th, 2007, 09:58 PM
Since you know you have only true and false as options you only have to count how many trues there are and you don't need to worry about the option group names. (if that is not a true assumption let me know)

So, you can do this. Test code uses 5 groups and looks for 3 or more.

<!doctype html public "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title> New Document </title>
<script type="text/javascript">
function checkForm(frm)
{
var numTrueChecked = 0;

for (var i = 0; i < frm.elements.length; i++)
{
if (frm.elements[i].type == "radio")
{
if (frm.elements[i].value == "true" &&
frm.elements[i].checked)
numTrueChecked ++;
}
}

return (numTrueChecked >= 3);
}
</script>
</head>

<body>
<form name="frm1">
Question 1 <br />
true <input type="radio" name="q1" value="true"><br />
false <input type="radio" name="q1" value="false"><br />

Question 2 <br />
true <input type="radio" name="q2" value="true"><br />
false <input type="radio" name="q2" value="false"><br />

Question 3 <br />
true <input type="radio" name="q3" value="true"><br />
false <input type="radio" name="q3" value="false"><br />

Question 4 <br />
true <input type="radio" name="q4" value="true"><br />
false <input type="radio" name="q4" value="false"><br />

Question 5 <br />
true <input type="radio" name="q5" value="true"><br />
false <input type="radio" name="q5" value="false"><br />


<input type="button" value="check it" onClick="alert(checkForm(this.form))" />
</form>
</body>
</html>

dajohnson1s
Sep 18th, 2007, 12:41 AM
Thank you nikkiH for your help it is much appriecated.

This is almost what I am hoping to do. I had this quiz originally coded as an asp.net project. And there is one feature of the quiz that I would like to add to your version. Once the submit button is clicked...the function is called...I would like to add a list box or text area to appear that I can add my own text instead of the alert box. With asp.net I just made a list box appear and I could add whatever text I wanted. I do not recall a similar feature in javascript, but I am very new to this language.

Thanks

dajohnson1s
Sep 18th, 2007, 06:33 AM
Would a div be the better way to go about displaying a hidden text field?

nikkiH
Sep 18th, 2007, 03:30 PM
Personally, I'd use a div with the text in it, set visibility to hidden, then when I wanted it displayed, set it to visible.

Well, honestly I'd use .NET, not javascript, but barring that... ;)