Connecting Tech Pros Worldwide Forums | Help | Site Map

I want to sum values in rows and coloums of a table.

Member
 
Join Date: Sep 2009
Location: London
Posts: 36
#1: Sep 15 '09
I want the result to be displayed at the last row and last colom of every row...

I have found this javascript code from somewhere.to do the same.. But the problem is . I am confused how should I call this function in my actual table rows and coloms...

Expand|Select|Wrap|Line Numbers
  1. <script language="javascript">
  2. function sumup( o ) {
  3.  var t = [], cell,
  4.   row = document.getElementById( 'sumtable' ).rows,
  5.   i = row.length - 1,
  6.   lastrow = row[i];
  7.  while(i--) {
  8.   cell = row[i].cells; j = cell.length;
  9.   while(j--) {
  10.    if( !t[j] ) { t[j] = 0; }
  11.    t[j] += parseFloat( cell[j].firstChild.nodeValue ) || 0;
  12.   }
  13.  }
  14.  j = t.length; while(j--) {
  15.   if( lastrow.cells[j] ) { lastrow.cells[j].firstChild.nodeValue = t[j]; }
  16.  }
  17.  o.disabled = true;
  18. }
  19. </script>
I know that I to the table I have to give id = sumtable .. but wht should I do to get the result in the last row and table...

PLease please help me... as i m new to javascript and programmin

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,660
#2: Sep 15 '09

re: I want to sum values in rows and coloums of a table.


Quote:

Originally Posted by tarunkhatri View Post

but wht should I do to get the result in the last row and table...

call the function with the desired event. the function takes care of the rest.
Member
 
Join Date: Sep 2009
Location: London
Posts: 36
#3: Sep 15 '09

re: I want to sum values in rows and coloums of a table.


Thanks for quick answer Dormilich. I am calling the function in the followin manner on the onchange event.. but is not working..

Expand|Select|Wrap|Line Numbers
  1. <table width="200" border="1" id = "sumtable">
  2. <tbody id = "sumtable>
  3.   <tr>
  4.     <td><input type="text" id="Row 1 colom 1" name="value1" value ="" size = "4"/ onChange = "sumup()"></td>
  5.     <td><input type="text" id="Row 1 colom 1" name="value2" value ="" size = "4" onChange = "sumup()"/></td>
  6.     <td><input type="text" id="Row 1 colom 1" name="value3" value ="" size = "4" onChange = "sumup()"/></td>
  7.     <td><input type="text" id="Row 1 colom 1" name="value4" value ="" size = "4" onChange = "sumup()"/></td>
  8.     <td><input type="text" id="answer" name="answer" value="" DISABLED/></td>
  9.   </tr>
  10.   <tr>
  11.     <td><input type="text" id="value1" name="value1" value ="0" size = "4" onChange = "sumup()"/></td>
  12.     <td><input type="text" id="value2" name="value2" value ="0" size = "4"/ onChange = "sumup()"></td>
  13.     <td><input type="text" id="value3" name="value3" value ="0" size = "4"/ onChange = "sumup()"></td>
  14.     <td><input type="text" id="value4" name="value4" value ="0" size = "4"/ onChange = "sumup()"></td>
  15.     <td><input type="text" id="answer" name="answer" value="" DISABLED/></td>
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,660
#4: Sep 15 '09

re: I want to sum values in rows and coloums of a table.


the programme expects to have the values to count as text of the table cells, not as input fields.

counting the values of input fields is much easier, because you can get the fields by a common name.

sumup() is missing the input parameter

PS. please use tags when posting code

PPS. the IDs in the first <tr> are invalid.
Member
 
Join Date: Sep 2009
Location: London
Posts: 36
#5: Sep 15 '09

re: I want to sum values in rows and coloums of a table.


Hummm.... You are rgt I want the values to be input types .. and as soon as any value in the grid is changed I want the sum to e updated......

Even I want to get all the fields by common name.... But if u can please help me how can I set the parameter in sumup() which is missing and than call it in the actual HTML .. As today is only my second day with javascrpt to vry much confused...
Many Thanks
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,660
#6: Sep 15 '09

re: I want to sum values in rows and coloums of a table.


Quote:

Originally Posted by tarunkhatri View Post

As today is only my second day with javascrpt to vry much confused...

then it’s best to read a tutorial, there are some links in the forum stickies.

Quote:

Originally Posted by tarunkhatri View Post

But if u can please help me how can I set the parameter in sumup() which is missing and than call it in the actual HTML ..

you need a different function because this function doesn’t fit your needs.
Member
 
Join Date: Sep 2009
Location: London
Posts: 36
#7: Sep 15 '09

re: I want to sum values in rows and coloums of a table.


Ohh .. is it... I appriatiate your effort thanks very much..... But if you find a function thts does this job please reply...As I have a deadline of today from my boss :( ...
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,660
#8: Sep 15 '09

re: I want to sum values in rows and coloums of a table.


no need for me to search, I could write something as simple as that in probably 15 mins.
Member
 
Join Date: Sep 2009
Location: London
Posts: 36
#9: Sep 15 '09

re: I want to sum values in rows and coloums of a table.


Can u please please do it for me .. If u have 15 mins ..It wud be a gr8 gr8 help for me .....
Reply

Tags
coloum sum, javascript, row sum, sum, table sum