total function

B

beavenour

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>
 
G

Grant Wagner

beavenour said:
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.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].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 <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_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-developer/upgrade_2.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,575
Members
47,207
Latest member
HelenaCani

Latest Threads

Top