473,399 Members | 3,038 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Need help with Javascript radio buttons (values)

3
Hello,

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

seem to make it work:

[HTML]<HTML><HEAD><TITLE>Radio Buttons</TITLE>
<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 }
5
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 }
10
11 document.write("The sum is " + parseInt(Num1) + parseInt12(Num2));
13}
14</SCRIPT>

<BODY>
<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>
<BR>
<INPUT onclick=process() type=button value=Add>
<BR></FORM></BODY></HTML>[/HTML]

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
3 1992
ronverdonk
4,258 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.

Ronald
Feb 27 '08 #2
norbit
3
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)));
  2.  
thank again :)
Feb 28 '08 #3
ronverdonk
4,258 Expert 4TB
You are welcome. See you next time.

Ronald
Feb 28 '08 #4

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

Similar topics

2
by: Newbie | last post by:
I currently have a set of simple calculations to determine square footage and multuply that by a dollar amount per foot. I use form fields that are filled in by the user, and then the Submit...
11
by: Jon Hoowes | last post by:
Hi, I have inherited some code that has some form elements (radio buttons) that are called "1", "2" etc. for example: <input name="2" type="radio" value="45"> <input name="2" type="radio"...
2
by: Satheesh Babu B | last post by:
hai.. am having a checkboxlist..now how do i get the value of the checkbox that is checked in checkboxlist using javascript? Thanks in advance.... Regards, Satheesh
3
by: Amelyan | last post by:
When we want radio button to belong to a group name we say, radio1.GroupName="GroupA". In this case, radio1 will be unselected if another radio button is selected in "GroupA". Is there a way...
18
by: Ed Jay | last post by:
<disclaimer>js newbie</disclaimer> My page has a form comprised of several radio buttons. I want to poll the buttons to determine which button was selected and convert its value to a string. I...
2
by: clinttoris | last post by:
Hello, If someone could help me it would be appreciated as I am not having much luck. I'm struggling with my asp code and have some questions relating to asp and oracle database. First...
6
by: eureka | last post by:
Hi friends, I am developing a web application using Jsp and JS. I have a main Jsp page(Jsp1).Inside it I have an iframe having an Html- table which is created dynamically and contains all...
1
by: Mufasa | last post by:
I have a couple of radio buttons that make various things appear/disappear on the screen through JavaScript. All works great. Problem is I'll click a radio button, something will appear, I reload...
2
by: ghjk | last post by:
I have 2 radio buttons in php file and I want to send values of them to a javascript. When I put same ID name to both radiobuttions, it doesn't work. But javascript only get values using the id....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.