Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript_howto thread: form calculation


Message #1 by "sridarti herryanto" <sridarti@s...> on Thu, 11 Apr 2002 04:14:03
<-- How to use this functioin -->
<-- Change any thing in [] to what you need for your page -->

<script language="JavaScript" type="text/javascript">
var num_elements = window.document.[form name here].elements.length;

<!-- num_elements will tell you the total elements in your form-->

var nun_Of_Cols = (num_elements/[change this number]);

<!-- Number of column = days-of-the-week 7 work days 5?  This will take the
total number of elements and divide that into columns and row. The function
addTheFields will go through the form elements adding them up then when it
hits num_Of_Cols it will place that totaled number there. Reset rowVal to 0
and start adding the next row up. You could make a simular function to add
rows up.  I have one that add over 300 form objects up.  Total number does
not matter the only thing that matter is that you can give this function a
column count. Example Col=5 a+b+c+d=e  I hope that helps if you have an
example feal free to post it to HowTo :) -->

function addTheFields() {
  for (i=0; i<num_elements; i++) {
  rowVal = 0;
    for (j=0; j<(nun_Of_Cols-1); j++) {
      rowVal = rowVal + new Number(getVal(document.form2.elements[i]));
      i++;
      }
    document.[form name here].elements[i].value = rowVal;
    }
  }

function getVal(elem) {
  if ((elem.value == "") || (elem.value == null) || (elem.value == " "))
    return 0;
  else
    return elem.value;
  }
alert("there are " + num_elements+ " here")
</script>

-----Original Message-----
From: Terrance Atkins [mailto:terrance.atkins@u...]
Sent: Tuesday, April 23, 2002 8:09 PM
To: JavaScript HowTo
Subject: [javascript_howto] Re: form calculation


Hi Garrett,

I have a similar request.  I need to provide a row total to a time card
page.  For example, I have rows of textboxes labeled mon, tues, wed, etc.
I need to display a row total at the right end of the row that sums the M-
F totals.  I need this to be performed for each row.  The number of rows
will differ each week.  I need to capture the exact number of rows on the
page and then apply the totals as the user changes a textbox.

Any additional hints/suggestions would be appreciated.

Thank You

---


  Return to Index