total function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • beavenour

    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(myfie ld01, myfield02)
    at one total field and calculate(myfie ld06, 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(docume nt.myform.myfie ld01.value);
    b = parseInt(docume nt.myform.myfie ld02.value);
    c = parseInt(docume nt.myform.myfie ld03.value);
    d = parseInt(docume nt.myform.myfie ld04.value);

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

    #2
    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(myfie ld01, myfield02)
    > at one total field and calculate(myfie ld06, 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(docume nt.myform.myfie ld01.value);
    > b = parseInt(docume nt.myform.myfie ld02.value);
    > c = parseInt(docume nt.myform.myfie ld03.value);
    > d = parseInt(docume nt.myform.myfie ld04.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.lengt h; i++) {
    total += parseInt(argume nts[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="Calculat e"
    onclick="calcul ate(this.form.t otal, this.form.field 01, this.form.field 03,
    this.form.field 04);">
    </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.lengt h; i++) {
    total += parseInt(theFor m.elements[arguments[i]].value, 10);
    }

    theForm.element s[theResult].value = total;
    }

    and it could be called with:

    <input type="button" value="Calculat e" onclick="calcul ate(this.form,
    'total', 'field01', 'field03', 'field04');">

    Which specific implementation you choose depends on your requirements.

    --
    | Grant Wagner <gwagner@agrico reunited.com>

    * Client-side Javascript and Netscape 4 DOM Reference available at:
    *


    * Internet Explorer DOM Reference available at:
    *
    Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


    * 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


    Comment

    Working...