radio button values

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
thatsme
Forum Commoner
Posts: 87
Joined: Sat Apr 07, 2007 2:18 am

radio button values

Post by thatsme »

feyd | Please use

Code: Select all

,

Code: Select all

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?


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

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]
thatsme
Forum Commoner
Posts: 87
Joined: Sat Apr 07, 2007 2:18 am

Post by thatsme »

feyd | Please use

Code: Select all

,

Code: Select all

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>
I am calling this function onsubmit in form.

I am not getting correct answer


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

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]
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Use parseInt() to get the value from inputs. I suppose that in your case you have a string concatenation, instead of arithmetic sum.
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply