473,508 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>
Jul 20 '05 #1
1 10802
beavenour wrote:
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>


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 <gw*****@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
Jul 20 '05 #2

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

Similar topics

4
11787
by: James Greig | last post by:
hello people, i'm just learning javascript, could someone point me in the direction of an example of the following, or give me some clues as to how it might be done: what i would like to do...
7
2816
by: rick | last post by:
Can anyone help, I am try to create a simple form using a table, where a user can fill out quanty and price and have a total automatically calculated and inserted in another field. I stuck trying...
1
6069
by: bin_P19 P | last post by:
the code i have got is as follows and now im stuck <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Shopping...
1
1791
by: Samir | last post by:
Here is a sample form that I just made up. I would like a somekind of script that when each button is checked or unchecked to dynamicly add or subtract the total. If anyone can help, thanks alot....
10
5955
by: giancarlodirisioster | last post by:
Can someone help me modify this for future Usenet archival and to help me solve what I don't know? <form name="addform" method="POST" action="./submit.php"> <input type="text" name="Box 1"...
1
1894
by: webguy262 | last post by:
I'm trying to modify this script... <script language="JavaScript" type="text/javascript"> <!-- /* This script is Copyright (c) Paul McFedries and Logophilia Limited...
4
6352
by: Rich_C | last post by:
I'm sure this is very simple, but I have very little experience with javascript -- and what I do know isn't helping me here. I have a simple form where users can enter a quantity (qty) and cost...
2
2187
by: HarisHohkl | last post by:
Hi, I've this function in a class to update the total value.but when i try to remove the these row highlight in Bold it crash, what should i do???? void display_total_value() { double...
2
5041
by: sammiesue | last post by:
Hi, I have form with 2 autosummed textboxes ("total" and "casinototal"). I would like to have a grand total textbox ("grandtotal") get its value from summing "total" and "casinototal", but it...
0
7231
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
7405
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
7504
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
5643
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,...
1
5059
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...
0
4724
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...
0
3214
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...
0
1568
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 ...
1
773
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.