Connecting Tech Pros Worldwide Help | Site Map

total function

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 20th, 2005, 09:44 AM
beavenour
Guest
 
Posts: n/a
Default total function

I am looking for some help creating a more universal function out of
this lame attempt at javascript. I have little experience with
javascript and want to be able to universalize this function. So how
do I change this script so that I can call any field or any number of
fields to be totaled? For example - calculate(myfield01, myfield02)
at one total field and calculate(myfield06, myfield08, myfield11) at
another total field instead of creating a whole new function to do
each. Any help or suggestions would be very much apprectated.


<SCRIPT language = JavaScript>
function calculate() {

a = parseInt(document.myform.myfield01.value);
b = parseInt(document.myform.myfield02.value);
c = parseInt(document.myform.myfield03.value);
d = parseInt(document.myform.myfield04.value);

z = Math.round((a + b + c + d)*100)/100;
document.myform.total.value = z;
}
</SCRIPT>

  #2  
Old July 20th, 2005, 09:47 AM
Grant Wagner
Guest
 
Posts: n/a
Default Re: total function

beavenour wrote:
[color=blue]
> I am looking for some help creating a more universal function out of
> this lame attempt at javascript. I have little experience with
> javascript and want to be able to universalize this function. So how
> do I change this script so that I can call any field or any number of
> fields to be totaled? For example - calculate(myfield01, myfield02)
> at one total field and calculate(myfield06, myfield08, myfield11) at
> another total field instead of creating a whole new function to do
> each. Any help or suggestions would be very much apprectated.
>
> <SCRIPT language = JavaScript>
> function calculate() {
>
> a = parseInt(document.myform.myfield01.value);
> b = parseInt(document.myform.myfield02.value);
> c = parseInt(document.myform.myfield03.value);
> d = parseInt(document.myform.myfield04.value);
>
> z = Math.round((a + b + c + d)*100)/100;
> document.myform.total.value = z;
> }
> </SCRIPT>[/color]

I'm not sure what you actually want to pass to the function, but you have
some alternatives. I suppose I'd probably pass references to the form
elements to be totalled to the function, along with a reference to
element which will eventually contain the total:

<script type="text/javascript">
function calculate() {
var total = 0;

for (var i = 1; i < arguments.length; i++) {
total += parseInt(arguments[i].value, 10);
}

arguments[0].value = total;
}
</script>
<form name="myForm">
<input type="text" name="field01" value="1">
<input type="text" name="field02" value="2">
<input type="text" name="field03" value="3">
<input type="text" name="field04" value="4">
<input type="text" name="total" value="0">
<input type="button" value="Calculate"
onclick="calculate(this.form.total, this.form.field01, this.form.field03,
this.form.field04);">
</form>

You could also pass a reference to the form and the names of the fields:

function calculate() {
var total = 0;
var theForm = arguments[0];
var theResult = arguments[1];

for (var i = 2; i < arguments.length; i++) {
total += parseInt(theForm.elements[arguments[i]].value, 10);
}

theForm.elements[theResult].value = total;
}

and it could be called with:

<input type="button" value="Calculate" onclick="calculate(this.form,
'total', 'field01', 'field03', 'field04');">

Which specific implementation you choose depends on your requirements.

--
| Grant Wagner <gwagner@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.