473,767 Members | 1,627 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with Javascript radio buttons (values)

3 New Member
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><TI TLE>Radio Buttons</TITLE>
<SCRIPT language=JavaSc ript>

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.fo rm1.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 2019
ronverdonk
4,258 Recognized Expert Specialist
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 New Member
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 Recognized Expert Specialist
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
8177
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 button opens the asp page that returns the result of the calculation. What I would like to do is add certain numbers to the total based on radio buttons or checkboxes that the user activates on the form page. For instance, they enter the numbers into...
11
2257
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" value="46">
2
13708
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
2072
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 (trick, custom RadioButton, or javascript) to make radio button (radio1) belong to 2 independent radio button groups instead of one? This would be an equivalent of sayting something like radio1.GroupName1 = "GroupA"; radio1.GroupName2 = "GroupB";
18
2269
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 then want to use the string on the same page. My script is: function checkRadio(field) { for(var i=0; i < field.length; i++) {
2
4722
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 question. I have a web survey that I am working on and have successfully dynamically taken the info from a database, displayed it on the screen and then taken the users answers and inserted them into a
6
2536
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 records from the backend-table . For creating this dynamic table I've used another Jsp(Jsp2) and in Jsp1 I say: <iframe id = "I1" src="Jsp2.jsp" width=320 scrolling="yes">
1
5466
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 the page for other reasons and the item disappears. The reason it disappears is I have to set the state initially to invisible so it doesn't show up initially. The radio buttons are html radio buttons not aspx radio buttons so I can have the...
2
5749
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. How can I do that? php code -------------- <input name="Gender" id="Gender" type="radio" value="1" /> Male <input name="Gender" id="Gender" type="radio" value="2" />Female&nbsp; javascript code ------------------- var...
0
10169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10013
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9841
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8838
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7383
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6655
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5280
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5424
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2807
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.