473,414 Members | 1,618 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

adding column numbers

11
Hello,
I have a table with an add-row setup. The table has several columns but I only need to calculate COLUMN FOUR. The results go to a textbox via a button. My code is.....

Expand|Select|Wrap|Line Numbers
  1. function sumup() {
  2.  document.getElementById("Table1")
  3.   column = document.getElementsByTagName("colFour");
  4.   document.myForm.tbTotal.value = column;
  5. }
.....
Expand|Select|Wrap|Line Numbers
  1. <TABLE NAME="Table1" id="Table1">
  2. <COL WIDTH=10*>
  3.     <COL WIDTH=139*>
  4.     <COL WIDTH=27*>
  5.     <COL WIDTH=29*>
  6.     <COL WIDTH=27* NAME="colFour" id="colFour">
  7.     <COL WIDTH=10*>
.....
Expand|Select|Wrap|Line Numbers
  1. <INPUT TYPE=TEXT NAME="tbTotal">
  2. <INPUT TYPE=BUTTON NAME="pbTest" VALUE="TEST" onclick="sumup();"./>
.....But when I click pbTest, the result in tbTotal is, with the brackets.....
[object]
.....What am I doing wrong?
Apr 17 '07 #1
7 1902
acoder
16,027 Expert Mod 8TB
You need to use getElementById instead.
Apr 18 '07 #2
hi pal,
im working on your problem whenever i have some time,

in firefox the price wont be written to the new cell created
this is easily fixed
use
Expand|Select|Wrap|Line Numbers
  1. // cell 4 - ***PRICE*** of record
  2. var itemCost = document.forms['R2CDorder1'].tbPrice.value;
  3. var cell4 = row.insertCell(4); 
  4. cell4.appendChild(document.createTextNode(itemCost)); 
  5.  
well peace m8
Apr 18 '07 #3
decren
11
You need to use getElementById instead
I assume you mean...

Expand|Select|Wrap|Line Numbers
  1. column = document.getElementBy("colFour");
...I did not realize you could use getElementById consecutively... Thanks, I'll try to work that out.

im working on your problem whenever i have some time,
I appreciate any help on this. I think its turning out to be a decent piece of work in progress but I hope not to paint myself into a corner... and thank you for the heads up in firefox.

...To anyone else, The project I'm working on is at:

[HTML]http://www.aaronzjukebox.com/R2CDorder1[/HTML]

but the problem is associated with Table4 instead of Table1. Any and all pointers are appreciated.
Apr 18 '07 #4
mrhoo
428 256MB
This is a general function to total the cells in a table column.
It assumes that all of the rows are in a single tbody,
though it is easy enough to call it on multiple tbodies and total the result.
Empty cells in the column and cells without numbers are treated as 0.

You can call the function with no arguments, columnSum();
and it will return the total of the last column of the first tbody on the page,
or you can specify the parent table or tbody and the column number.
Expand|Select|Wrap|Line Numbers
  1. function columnSum(pa,col){
  2.     pa= pa || document.getElementsByTagName('tbody')[0];
  3.     /* if no parent is passed, get the first tbody on the page */
  4.  
  5.     var G= pa.getElementsByTagName('tr');
  6.     col= col || G[0].getElementsByTagName('td').length;
  7.     --col;
  8.     /* if no col is passed, get the index of the last column
  9.     note- not 0 based, the argument for the first column is passed as 1 */
  10.  
  11.     var L= G.length,sum= 0,temA= [];
  12.     for(var i= 0; i< L; i++){
  13.         tem= G[i].getElementsByTagName('td')[col];
  14.         var c=0;
  15.         while(tem && !tem.data ) tem= tem.childNodes[c++];
  16.          /* digs into the cell's innards, in case the data is in a child of the cell */
  17.         tem= tem? tem.data : 0;
  18.         sum+= parseFloat(tem) || 0;
  19.     }
  20.     return sum;
  21. }
Apr 18 '07 #5
decren
11
=DutchKingCobra
in firefox the price wont be written to the new cell created
this is easily fixed
use
Expand|Select|Wrap|Line Numbers
  1. // cell 4 - ***PRICE*** of record
  2. var itemCost = document.forms['R2CDorder1'].tbPrice.value;
  3. var cell4 = row.insertCell(4); 
  4. cell4.appendChild(document.createTextNode(itemCost)); 
  5.  
well peace m8
I downloaded FireFox to see what was going wrong. My problem was that I had NAMEd all my elements in the table but had not assigned IDs. This worked fine in IE but halted the function in FireFox. Thanx for the heads-up.
May 5 '07 #6
decren
11
I want to thank everyone at TheScripts for their input to this problem. With your help and with a table located at:
http://4umi.com/web/javascript/
I was able to work out a solution. If anyone is interested in (what I believe to be unique) addRow/shoppingCart type form, you are welcome to take what you need from:
http://www.aaronzjukebox.com/R2CDorder1
Understand though that it is a work in progress so you will have to sift through a lot of garbage to get the scripts to your particular need.
And thanx again
May 5 '07 #7
pbmods
5,821 Expert 4TB
Wouldn't it make more sense just to add an onchange handler to each of the inputs you want to total? Something like...

[HTML]
<td><input id="row1Price" onchange="sumRows(this);" /></td>
[/HTML]

Expand|Select|Wrap|Line Numbers
  1. var $rowValues = {};
  2. function sumRows($element) {
  3.     $element.value = $element.value.replace(/[^\d.]/, '');
  4.     $rowValues[$element.id] = $element.value;
  5.  
  6.     document.getElementById('theSum').value = sum($rowValues);
  7. }
  8.  
  9. function sum($obj) {
  10.     var $retVal = 0;
  11.  
  12.     for($r in $obj)
  13.         $retVal += $obj[$r];
  14.  
  15.     return $retVal;
  16. }
  17.  
May 5 '07 #8

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

Similar topics

10
by: sp0 | last post by:
Is there a reason why to make mix numbers improper when adding? It seems when subtracting and adding, adding a subtracting the whole numbers and fraction parts should be sufficient? what'ch think
20
by: Steve Jorgensen | last post by:
Hi all, I've just finished almost all of what has turned out to be a real bear of a project. It has to import data from a monthly spreadsheet export from another program, and convert that into...
3
by: Robin Thomas | last post by:
I am fairly new to ASP.NET so I think I am missing something fundamental. Anyway, quite often I am pulling data from a database, but then I need to use that data to produce more data. A simple...
12
by: Art | last post by:
Hi everyone I was hoping someone might be able to help me with this. I'm just starting to try to work with MS Access tables through VB.net. In Access I can take an existing table and add a new...
6
by: dbuchanan | last post by:
Hello, Is this a bug? Is there some kind of work around? I want to add default values for a few columns in my datagridview I found the "DefaultValuesNeeded" event for the datagridview I...
17
by: Sri | last post by:
How do you add an n-bit number in C? Regards, Sri
6
by: nogimmies | last post by:
I have a column named CardNumber in a table named Cards (formatted as varchar) with numbers ranging from 1 to 65534. I am trying to change all the card numbers to start with 84 and fill with 0's...
3
by: Jx4 | last post by:
Hi there, I am currently having trouble with some issues with adding Currency values. I have one column with values of transactions and the other column has a running total. It works great...
60
by: Bill Cunningham | last post by:
I have a row of values like such, placed in a text file by fprintf. 10.50 10.25 10.00 10.75 11.00 What I want to do to the above colum is add a new column right beside it which is a total...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.