473,404 Members | 2,187 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,404 software developers and data experts.

Getting correct value from radio button

Hello all,

I am new to the group and new to javascripting, so I am hoping to find
some good help here.

Here is a snippet of my code:

HTML:
Name: <input type="text" name="ageName0" id="ageName0" />
<input type="radio" value="0" name="age0" onclick="getPrice();" /6
and under
<input type="radio" value="20" name="age0" onclick="getPrice();" /7
to 12
<input type="radio" value="50" name="age0" onclick="getPrice();" /13
and over
Amount: $ <input type="text" name="ageAmount0" id="ageAmount0" /><br>
....
....
Name: <input type="text" name="ageName6" id="ageName6" />
<input type="radio" value="0" name="age6" onclick="getPrice();" /6
and under
<input type="radio" value="20" name="age6" onclick="getPrice();" /7
to 12
<input type="radio" value="50" name="age6" onclick="getPrice();" /13
and over
Amount: $ <input type="text" name="ageAmount6" id="ageAmount6" /><br>
javascript:
var allAges = new
Array(thisAge0,thisAge1,thisAge2,thisAge3,thisAge4 ,thisAge5,thisAge6);
var allAgesAmounts = new
Array(thisAgeAmount0,thisAgeAmount1,thisAgeAmount2 ,thisAgeAmount3,thisAgeAmount4,thisAgeAmount5,this AgeAmount6);

function getPrice()
{
for (var i = 0; i < allAges.length; i++)
{
allAgesAmounts[i].value = "";
for (var j = 0; j < allAges[i].length; j++)
{
if (allAges[i][j].checked == true)
{
allAgesAmounts[i].value = allAges[i][j].value;
console.log(allAgesAmounts[i].value);
}
}
}
....
....
}

The issue I am having is that when I click on the first set of radio
buttons, I am presented with 1 value, but if I click on the second set
of radio button, I am presented with 2 values instead of 1. The same
goes for the third, fourth, fifth and sixth where I a presented with
3, 4, 5 and 6 values, respectively.

You can see what I am referring to by going to this link
http://www.wootenfamilyreunion.org/6a.php.

Is there a way to stop this from happening?

Thanks for your time and patience.
Jun 27 '08 #1
4 2248
SAM
Bernie a écrit :
Hello all,

I am new to the group and new to javascripting, so I am hoping to find
some good help here.
<form action="#">
Name: <input type="text" name="ageName0" id="ageName0" />
<input type="radio" value="0" name="age0" onclick="getPrice(this);" /6
and under
<input type="radio" value="20" name="age0" onclick="getPrice(this);" /7
to 12
<input type="radio" value="50" name="age0" onclick="getPrice(this);" /13
and over
Amount: $ <input type="text" name="ageAmount0" id="ageAmount0" /><br>

.....

Your Total = <input name="totalAmount" id="totalAmont"><br>
<input type=reset><input type=submit>
</form>
JS :
====

function getPrice(what) {
var f = what.form; // which form it is
var age = what.name; // name of the radio
what = f[age]; // collection of radios
age = age.substring(age.indexOf('e')+1); // number (index)
for(var i=0; i<what.length; i++)
if(what[i].checked) f['ageAmount'+age].value = what[i].value ;
total(f);
}
function total(f) {
var t = 0, L = (f.length-3)/5; // or L = 6 for your example
for(var i=0; i<L; i++) t += f['ageAmount'+i].value*1;
f.totalAmount.value = t;
}

The issue I am having is that when I click on the first set of radio
buttons, I am presented with 1 value, but if I click on the second set
of radio button, I am presented with 2 values instead of 1. The same
goes for the third, fourth, fifth and sixth where I a presented with
3, 4, 5 and 6 values, respectively.
the values of inputs are texts (type=text) and not numbers
so you add the values as texts
You must to convert them in numbers

var price = foo1.value *1 + foo2.value*1 + foo3.value*1;

--
sm
Jun 27 '08 #2
On Jun 17, 2:50*pm, SAM <stephanemoriaux.NoAd...@wanadoo.fr.invalid>
wrote:
Bernie a écrit :
Hello all,
I am new to the group and new to javascripting, so I am hoping to find
some good help here.

<form action="#">
Name: <input type="text" name="ageName0" id="ageName0" />
<input type="radio" value="0" name="age0" onclick="getPrice(this);" /6
and under
<input type="radio" value="20" name="age0" onclick="getPrice(this);" /7
to 12
<input type="radio" value="50" name="age0" onclick="getPrice(this);" /13
and over
Amount: $ <input type="text" name="ageAmount0" id="ageAmount0" /><br>

....

Your Total = <input name="totalAmount" id="totalAmont"><br>
<input type=reset><input type=submit>
</form>

JS :
====

function getPrice(what) {
* *var f = what.form; * *// which form it is
* *var age = what.name; *// name of the radio
* *what = f[age]; * * * *// collection of radios
* *age = age.substring(age.indexOf('e')+1); *// number (index)
* *for(var i=0; i<what.length; i++)
* *if(what[i].checked) f['ageAmount'+age].value = what[i].value ;
* *total(f);}

function total(f) {
* var t = 0, L = (f.length-3)/5; *// or L = 6 for your example
* for(var i=0; i<L; i++) t += f['ageAmount'+i].value*1;
* f.totalAmount.value = t;

}
The issue I am having is that when I click on the first set of radio
buttons, I am presented with 1 value, but if I click on the second set
of radio button, I am presented with 2 values instead of 1. The same
goes for the third, fourth, fifth and sixth where I a presented with
3, 4, 5 and 6 values, respectively.

the values of inputs are texts (type=text) and not numbers
so you add the values as texts
You must to convert them in numbers

var *price = foo1.value *1 + foo2.value*1 + foo3.value*1;

--
sm
Thanks for replying back, SM.
My next question to you is in the line - "var t = 0, L = (f.length-3)/
5; // or L = 6 for your example", where does the 3 and 5 come from?
What are they referencing? I understand that f.length is the total
amount of element in the form, but the 3 and 5 has me stumped.

Thanks

B
Jun 27 '08 #3
SAM
Bernie a écrit :
>
Thanks for replying back, SM.
My next question to you is in the line - "var t = 0, L = (f.length-3)/
5; // or L = 6 for your example", where does the 3 and 5 come from?
What are they referencing? I understand that f.length is the total
amount of element in the form, but the 3 and 5 has me stumped.
you have 5 inputs by line
you would have to have 3 inputs in end of your form
- total
- reset
- submit

L = the form length (numbers of its elements (input, textarea) )

As you have a sub-total in each line in 5th position
I do a loop (L-3)/5 times

If that disturb you, you can loop on the whole form
searching elements with 'ageAmount' in their names :

var t = 0;
for(var i=0, L=f.length; i<L; i++) {
if(f[i].name.indexOf('ageAmount')>=0) t += f[i].value*1:
}
does the name content 'ageAmount' ?
name.indexOf('ageAmount')>=0

--
sm
Jun 27 '08 #4
On Jun 18, 3:04*pm, SAM <stephanemoriaux.NoAd...@wanadoo.fr.invalid>
wrote:
Bernie a écrit :
Thanks for replying back, SM.
My next question to you is in the line - "var t = 0, L = (f.length-3)/
5; *// or L = 6 for your example", where does the 3 and 5 come from?
What are they referencing? *I understand that f.length is the total
amount of element in the form, but the 3 and 5 has me stumped.

you have 5 inputs by line
you would have to have 3 inputs in end of your form
- total
- reset
- submit

L = the form length (numbers of its elements (input, textarea) )

As you have a sub-total in each line in 5th position
I do a loop *(L-3)/5 *times

If that disturb you, you can loop on the whole form
searching elements with 'ageAmount' in their names :

var t = 0;
for(var i=0, L=f.length; i<L; i++) {
* *if(f[i].name.indexOf('ageAmount')>=0) t += f[i].value*1:
* *}

does the name content 'ageAmount' ?
* *name.indexOf('ageAmount')>=0

--
sm
No, that did not disturb me. I was just curious. Thanks again.
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Peter | last post by:
Hi all, Sorry if this is an obvious/common question, but I'm trying to get the value of a radio button using JavaScript. Where I can easily get the value of a text field: ...
4
by: Randell D. | last post by:
Folks, I have a form (called FORM1) - In my INPUT submit tag, I have an onClick event that I have successfully tested/used to display the value of a TEXT box using the following function (called...
2
by: jason | last post by:
The following (likely far from imperfect code), reports a value of NaN in the j4 display. I suppose the problem is I am not really passing the "checked" value of the radio button via .value ......
4
by: mitch-co2 | last post by:
What I am trying to do is when someone clicks on the YES radio button I want the text field called MYTEXT to equal the text field named DATE. The below code works as long as I do NOT UN-COMMENT...
1
by: MickG | last post by:
I am trying to change the value of the variable "hard" according to which radio button is pressed and I am having no joy. Could anyone help me with this, the problematic section is marked with...
1
by: Rage Matrix | last post by:
Hi all, I have a small problem with Access radio button groups in VBA. I've got a radio button group called fraSearchMode with three radio buttons in it. At a certain point, I want to see which...
6
by: sgottenyc | last post by:
Hello, If you could assist me with the following situation, I would be very grateful. I have a table of data retrieved from database displayed on screen. To each row of data, I have added...
1
by: querry | last post by:
`Hi all, I am trying to get the selected radio button item in a radio button list using javascript. I am successful at doing this, but the problem is I have several Radio Button Lists, and the...
10
by: onetruelove | last post by:
I want to creat a post like this blog: http://onlinetoefltest.blogspot.com/2007/08/level-c-lesson-1.html When you chose all the answers and click show answer a msg box will appear and tells how...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
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,...
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.