473,545 Members | 529 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to make function cover all possible radio buttons?

I Have to sets of Radio buttons like so:
<input type="radio" name=p1 value=1>
<input type="radio" name=p1 value=2>
<input type="radio" name=p1 value=3>
<br>
<input type="radio" name=p2 value=1>
<input type="radio" name=p2 value=2>
<input type="radio" name=p2 value=3>

then a text area and a button:
<input size=7 name=total>
<input type="button" onclick=c()>

the function checks for the radio buttons then outputs its value in text
area.
<script type="text/javascript">
function c()
{if (p1[a].checked&&p2[b].checked)
total.value=par seFloat(p1[a].value)+parseFl oat(p2[b].value)
}</script>

my question is what is the possibility of giving values to a and b such as
all radio buttons can be covered, is there a way to do this without having
to output all cases with else if structure.


Jul 23 '05 #1
4 3260
"Oscar Monteiro" <of**@hotmail.c om> wrote in message
news:41******** *************@n ews.telepac.pt. ..
I Have to sets of Radio buttons like so:
<input type="radio" name=p1 value=1>
<input type="radio" name=p1 value=2>
<input type="radio" name=p1 value=3>
<br>
<input type="radio" name=p2 value=1>
<input type="radio" name=p2 value=2>
<input type="radio" name=p2 value=3>

then a text area and a button:
<input size=7 name=total>
<input type="button" onclick=c()>

the function checks for the radio buttons then outputs its value in text
area.
<script type="text/javascript">
function c()
{if (p1[a].checked&&p2[b].checked)
total.value=par seFloat(p1[a].value)+parseFl oat(p2[b].value)
}</script>

my question is what is the possibility of giving values to a and b such as
all radio buttons can be covered, is there a way to do this without having
to output all cases with else if structure.

Is this what you're looking for?

<html>
<head>
<title>radios.h tm</title>
<script type="text/javascript">
function c() {
var form = document.form1;
var p1_a = 0;
var p2_b = 0;
for (var a=0; a<form.p1.lengt h; a++) {
if (form.p1[a].checked) {
p1_a = parseFloat(form .p1[a].value);
}
}
for (var b=0; b<form.p2.lengt h; b++) {
if (form.p2[b].checked) {
p2_b = parseFloat(form .p2[b].value);
}
}
if (p1_a > 0 && p2_b > 0) {
form.total.valu e = p1_a + p2_b;
} else {
form.total.valu e = "";
}
}
</script>
</head>
<body>
<form action="" method="post" name="form1">
<input type="radio" name="p1" value="1">
<input type="radio" name="p1" value="2">
<input type="radio" name="p1" value="3">
<br>
<input type="radio" name="p2" value="1">
<input type="radio" name="p2" value="2">
<input type="radio" name="p2" value="3">
<br>
<input size="7" name="total">
<input type="button" onclick="c()">
</form>
</body>
</html>
Jul 23 '05 #2
Oscar Monteiro wrote:
my question is what is the possibility of giving values to a and b
such as all radio buttons can be covered, is there a way to do this
without having to output all cases with else if structure.
Here is an example to play with

<script type="text/javascript">
function radioValue(oRad io) {
if (oRadio && !oRadio.length && oRadio.value)
return oRadio.checked? oRadio.value : 0;
else if (oRadio && oRadio.length)
for (var i=0; i<oRadio.length ; i++)
if (oRadio[i].checked && oRadio[i].value)
return oRadio[i].value;
return 0;
}
function rAdd(aRadios) {
var nSum = 0;
if (aRadios && aRadios.length)
for (var i=0; i<aRadios.lengt h; i++) {
var nVal = parseInt(radioV alue(aRadios[i]));
if (!isNaN(nVal)) nSum += nVal;
}
return nSum;
}
</script>
<form action=""><p>
P1 <input type="radio" name="p1" value="1">1
<input type="radio" name="p1" value="2">2
<input type="radio" name="p1" value="3">3<br>
P2 <input type="radio" name="p2" value="1">1
<input type="radio" name="p2" value="2">2
<input type="radio" name="p2" value="3">3<br>
<input size="7" name="total">
<input type="button" value="Add"
onclick="this.f orm.total.value = rAdd([this.form.p1, this.form.p2])"

</p></form>

ciao, dhgm
Jul 23 '05 #3


Oscar Monteiro <of**@hotmail.c om> wrote in message
news:41******** *************@n ews.telepac.pt. ..

my question is what is the possibility of giving values to a and b such as
all radio buttons can be covered, is there a way to do this without having
to output all cases with else if structure.


Loop through all elements and sum the values of those with type radio that
are checked.

For a form called 'f1' with a total field called 'total', call with:

<input type="button" onclick="addRad (document.f1, document.f1.tot al)">
<SCRIPT type="text/javascript">

function addRad(frm, resultHolder)
{
var elems = frm.elements,
fl = frm.elements.le ngth,
total = 0;

for(var i=0; i<fl; i++)
if(elems[i].type=='radio' && elems[i].checked && !isNaN(elems[i].value))
total+=parseFlo at(elems[i].value);

resultHolder.va lue=total;
}

</SCRIPT>
</BODY>
</HTML>

--
S.C.
http://makeashorterlink.com/?H3E82245A


Jul 23 '05 #4
Oscar Monteiro wrote:
I Have to sets of Radio buttons like so:
Hey, what a party, can I join in? :-)
<input type="radio" name=p1 value=1>
<input type="radio" name=p1 value=2>
<input type="radio" name=p1 value=3>
<br>
<input type="radio" name=p2 value=1>
<input type="radio" name=p2 value=2>
<input type="radio" name=p2 value=3>

then a text area and a button:
<input size=7 name=total>
<input type="button" onclick=c()>
*All* attributes should be quoted:

<input type="radio" name="p1" value="1">
...

According to the HTML spec, one button in a set of radio buttons
should always be selected and suggests browsers make the first
selected if none are set to "checked". Therefore you should
either make one selected, perhaps adding a "0" button as the
default.

Note that once a user selects one button in a set, they can't
(with your code) de-select a value even though the page when
loaded likely has no buttons selected.

[...] total.value=par seFloat(p1[a].value)+parseFl oat(p2[b].value)
There is no need to use parseFloat, you have set the values so
just use them (unless you expect errors in your code). To
convert the form element value (always passed as a string) to a
number, use "+" which, in the your code, would be:

total.value = +p1[a].value + p2[b].value;

or in mine:

total += +arguments[i][j].value;

You also can't access the form element "total" that way in
browsers other than IE. In your code, presuming you have named
your form "formName", it should be:

document.forms['formName'].elements['total'].value...

which can be abbreviated (provided there are no special
characters in the names) to:

document.formNa me.total.value. ..

The form should have a reset button and the text area should be
given a default value - if you don't want users messing with it,
make it readonly too.

[...] my question is what is the possibility of giving values to a and b such as
all radio buttons can be covered, is there a way to do this without having
to output all cases with else if structure.


Yes - but you don't need any values for a & b. The solution
below takes references to as many button sets as you like. The
other posters have various solutions, mine, of course, is better
because:

1. You can pass as many sets of radio buttons as you like, no
limit.

2. It only adds the sets you pass, not all of them.

3. It is concise, requiring minimal use of DOM so should be
pretty good cross-browser-wise.

It is based on passing references to the sets of buttons you
want added.
<html><head><ti tle>radio add</title>
<script type="text/javascript">
function c() {
var total = 0;
for ( var i=0, argLen = arguments.lengt h; i<argLen; i++){
for (var j=0, setLen=argument s[i].length; j<setLen; j++){
if (arguments[i][j].checked) {
total += +arguments[i][j].value;
}
}
}
return total;
}
</script>
</head><body>

<form name="form1" action="">
<input type="radio" name="p1" value="0" checked>0
<input type="radio" name="p1" value="1">1
<input type="radio" name="p1" value="2">2
<input type="radio" name="p1" value="3">3
<br>
<input type="radio" name="p2" value="0" checked>0
<input type="radio" name="p2" value="1">1
<input type="radio" name="p2" value="2">2
<input type="radio" name="p2" value="3">3

<input size="7" name="total" readonly>
<input type="button" value="Add 'em" onclick="
this.form.total .value = c(this.form.p1, this.form.p2);
">
</form>

</body>
</html>

Hope that helps!! :-p

--
Rob
Jul 23 '05 #5

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

Similar topics

2
5414
by: Rob | last post by:
Hi all, I've got multiple sets of radio button that are dynamically created in code and populated by a database query. The query returns about 20 recordsets with 3 radio buttons per recordset and I want to be able to retrieve the selected value from each of the sets of radio buttons.The names of each group of radio buttons is set by the...
2
11938
by: James P. | last post by:
Help, I need to display radio buttons on a form. The data is from SQL table: each row in each table is displayed as a radio button. I have multiple SQL tables so I understand I need to put them each in a GroupBox. All the examples I saw from the books or from the web show me how to add static radio buttons at design, or dynamically at...
4
19422
by: Ed Jay | last post by:
I use window.document.form1.myCheckbox.checked=false; to uncheck (reset) several checkboxes. What is an equivalent for resetting a set of interrelated radio boxes? -- Ed Jay (remove M to respond by email)
24
4478
by: John Gabriel | last post by:
CreateDialogIndirect() fires my Radio button click function without the radio button having being clicked. I am using VC++ 2003.NET. I believe this is yet another Microsoft bug. Have checked dialog resources and memory - both appear to be in order. Can anyone comment on this? Should CreateDialogIndirect() which is located in dlgcore.cpp ever...
6
4849
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of the html page controls the form fields that are required. It doesn't function like it's supposed to and I can leave all the fields blank and it still...
1
1460
by: sluster | last post by:
I'm looking to create a search box that creates different kinds of searches based on a radio button selection. The first stage of this process for me is setting it up so clicking on different radio buttons changes what the document.write() function displays (later I intend to replace the small bits of text/html I have document.write displaying to...
1
2398
by: Para | last post by:
Hi , I am working on a project using C/C++ in linux environment. Our program right now using the command line(console window) inputs and starts executing. command on console are to start program is, ./exe Please enter the selections( displays 1to 6 options) if either of them selected the program starts.
2
5881
by: dpazza | last post by:
Hi, I'm creating a quiz on using a form in VB 2005 express. I have four sets of questions and answers (labels and radio buttons) and I change between which set of questions is currently shown on the form by changing the visible state of the radio buttons and labels utilising back and next buttons. E.g. Next button makes current radio...
5
3589
by: mad.scientist.jr | last post by:
According to http://www.quirksmode.org/js/forms.html you need to look through a radio button's members to find the value: I tried writing a reusable function to return the value, see code at: http://www.geocities.com/usenet_daughter/javascript_radio.htm but for some reason I can only get a value if I hard code the control name.
0
7465
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7398
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7805
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...
1
7416
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5969
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...
1
5325
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...
0
4944
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...
1
1878
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.