and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I have a form in which there are 10 questions each having 4 answers (radio buttons). I want to the sum of all the answers which user has checked.
This is the present code i wrote,
[syntax="javascript"]<script language="javascript">
function myPersonalityValue()
{
var q1 = document.getElementById("q1").value;
var q2 = document.getElementById("q2").value;
var q3 = document.getElementById("q3").value;
var q4 = document.getElementById("q4").value;
var q5 = document.getElementById("q5").value;
var q6 = document.getElementById("q6").value;
var q7 = document.getElementById("q7").value;
var q8 = document.getElementById("q8").value;
var q9 = document.getElementById("q9").value;
var q10 = document.getElementById("q10").value;
var q11 = document.getElementById("q11").value;
var q12 = document.getElementById("q12").value;
$total = q1+q2+q3+q4+q5+q6+q7+q8+q9+q10+q11+q12;
alert($total);
}
</script>
iam getting wrong answers. Also is there a better way like looping through the elements and summing up their value?
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I have changed my code to:
[syntax="javascript"]function myPersonalityValue()
{
var total = 0;
for(x=0; x<document.personality_test.length; x++)
{
if (document.personality_test[x].checked)
total = eval(document.personality_test[x].value + total);
}
alert(total);
}
</script>
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]