467,927 Members | 1,845 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,927 developers. It's quick & easy.

Need help with Javascript radio buttons (values)


Iím new to learning JavaScript. I was trying out a simple script but I canít

seem to make it work:

<SCRIPT language=JavaScript>

function process()
1 for (i = 0; i < document.form1.Q1.length; i++)
2 {if (document.form1.Q1[i].checked == true)
3 {Num1 = document.form1.Q1[i].value;}
4 }
6 for (i=0; i < document.form1.Q2.length; i++)
7 {if(document.form1.Q2[i].checked == true)
8 {Num2 = document.form1.Q2[i].value;}
9 }
11 document.write("The sum is " + parseInt(Num1) + parseInt12(Num2));

<FORM name=form1>
<H4>First Set</H4>

<INPUT type=radio value=2 name=Q1>2<BR>
<INPUT type=radio value=1 name=Q1>1<BR>
<INPUT type=radio value=-1 name=Q1>-1<BR>
<INPUT type=radio value=-2 name=Q1>-2<BR>

<H4>Second Set</H4>

<INPUT type=radio value=2 name=Q2>2<BR>
<INPUT type=radio value=1 name=Q2>1<BR>
<INPUT type=radio value=-1 name=Q2>-1<BR>
<INPUT type=radio value=-2 name=Q2>-2<BR>
<INPUT onclick=process() type=button value=Add>

The result should come out as the arithmetic addition of the values of the

radio buttons, right? But its giving me the result of concatenation operation.

Am I missing a code/command? Thanks for the help
Feb 27 '08 #1
  • viewed: 1671
3 Replies
Expert 4TB
Next time you better enclose any code within the appropriate code tags! See the Posting Guidelines.

The writeln does no addition, it just echoes strings. Calculate the total and write that, e.g.[html]var total=parseInt(Num1)+parseInt(Num2);[/html]Then write 'total' to the screen.

Feb 27 '08 #2
Sorry about the messy first post, Ronald...
And thanks for the helpful reply, it worked..

I also found out that this can also work:

Expand|Select|Wrap|Line Numbers
  1. document.write("The sum is " + (parseInt(Num1) + parseInt(Num2)));
thank again :)
Feb 28 '08 #3
Expert 4TB
You are welcome. See you next time.

Feb 28 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Satheesh Babu B | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.