By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,317 Members | 1,297 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,317 IT Pros & Developers. It's quick & easy.

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

P: 36
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
Sep 15 '09 #1
Share this Question
Share on Google+
8 Replies


Dormilich
Expert Mod 5K+
P: 8,639
@tarunkhatri
call the function with the desired event. the function takes care of the rest.
Sep 15 '09 #2

P: 36
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>
Sep 15 '09 #3

Dormilich
Expert Mod 5K+
P: 8,639
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.
Sep 15 '09 #4

P: 36
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
Sep 15 '09 #5

Dormilich
Expert Mod 5K+
P: 8,639
@tarunkhatri
then itís best to read a tutorial, there are some links in the forum stickies.

@tarunkhatri
you need a different function because this function doesnít fit your needs.
Sep 15 '09 #6

P: 36
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 :( ...
Sep 15 '09 #7

Dormilich
Expert Mod 5K+
P: 8,639
no need for me to search, I could write something as simple as that in probably 15 mins.
Sep 15 '09 #8

P: 36
Can u please please do it for me .. If u have 15 mins ..It wud be a gr8 gr8 help for me .....
Sep 15 '09 #9

Post your reply

Sign in to post your reply or Sign up for a free account.