473,395 Members | 1,541 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,395 software developers and data experts.

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=parseFloat(p1[a].value)+parseFloat(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 3234
"Oscar Monteiro" <of**@hotmail.com> wrote in message
news:41*********************@news.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=parseFloat(p1[a].value)+parseFloat(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.htm</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.length; a++) {
if (form.p1[a].checked) {
p1_a = parseFloat(form.p1[a].value);
}
}
for (var b=0; b<form.p2.length; b++) {
if (form.p2[b].checked) {
p2_b = parseFloat(form.p2[b].value);
}
}
if (p1_a > 0 && p2_b > 0) {
form.total.value = p1_a + p2_b;
} else {
form.total.value = "";
}
}
</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(oRadio) {
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.length; i++) {
var nVal = parseInt(radioValue(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.form.total.value = rAdd([this.form.p1, this.form.p2])"

</p></form>

ciao, dhgm
Jul 23 '05 #3


Oscar Monteiro <of**@hotmail.com> wrote in message
news:41*********************@news.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.total)">
<SCRIPT type="text/javascript">

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

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

resultHolder.value=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=parseFloat(p1[a].value)+parseFloat(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.formName.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><title>radio add</title>
<script type="text/javascript">
function c() {
var total = 0;
for ( var i=0, argLen = arguments.length; i<argLen; i++){
for (var j=0, setLen=arguments[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
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...
2
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...
4
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...
24
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...
6
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...
1
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...
1
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...
2
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...
5
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:...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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...

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.