Adding the values in text boxes

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

    Adding the values in text boxes

    I have a large form, that has text boxes of numbers in rows and
    columns. I need to sum the values in the columns, and put the total at
    the bottom of the column. But I also need to sum the values in each
    row, and put the total at the end of the row.

    What is the javascript to sum the values to a few text boxes, and how
    would I write the script for these totals to run all at once?

    Thanks.
  • Erwin Moller

    #2
    Re: Adding the values in text boxes

    Frank wrote:
    [color=blue]
    > I have a large form, that has text boxes of numbers in rows and
    > columns. I need to sum the values in the columns, and put the total at
    > the bottom of the column. But I also need to sum the values in each
    > row, and put the total at the end of the row.
    >
    > What is the javascript to sum the values to a few text boxes, and how
    > would I write the script for these totals to run all at once?
    >
    > Thanks.[/color]

    Hi Frank,

    It is not complicated at all.
    You must just give every field a clear name and use some basic javascript to
    do the adding.

    for example:
    your fields have the following names:
    row1col1 row1col2 row1col3 row1col4 row1total
    row2col1 row2col2 row2col3 row2col4 row2total
    row3col1 row3col2 row3col3 row3col4 row3total
    totalcol1 totalcol2 totalcol3 totalcol4

    <script type="text/javascript">
    var numrows = 3;
    var numcols = 4;

    function dototalrows(){
    for (var row=1; row=<numrows; row++) {
    var totalrow = 0;
    for (var col=1; col=<numcols; col++) {
    totalrow += document.forms["yourformna me"]["row"+row+"col" +col].value;
    }
    document.forms["yourformna me"]["row"+row+"tota l"].value = totalrow;
    }
    }
    </script>

    do the same for cols.
    (I didn't check my code, so forgive me any typos.)

    Maybe add some errorchecking for invalid values.

    Good luck!
    reagrds,
    Erwin Moller

    Comment

    Working...