473,657 Members | 2,523 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

adding column numbers

11 New Member
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 1918
acoder
16,027 Recognized Expert Moderator MVP
You need to use getElementById instead.
Apr 18 '07 #2
DutchKingCobra
37 New Member
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 New Member
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.aaronzjukeb ox.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 Contributor
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 New Member
=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 New Member
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 Recognized Expert Expert
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="sumRo ws(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
2676
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
2755
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 normalized data. The task is made more difficult by the fact that the structure itself can vary from month to month (in well defined ways). So, I used the SQL-centric approach, taking vertical stripes at a time so that, for instance, for each...
3
1944
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 example would be: Let's say Column1=StartDate and Column2=EndDate. In addition to displaying Column1 and Column2, I need to do some calculations and display in as Column3. The calculations are easy and can be done in the code-behind. How to display...
12
3569
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 field with the type AutoNumber. I can then set this up as a key. Then if I go into the table I will see sequential numbers have been inserted into that field for me. Can I do this through VB.net I'd appreciate any help or alternative suggestions...
6
6406
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 gave it a try using the example given in
17
12772
by: Sri | last post by:
How do you add an n-bit number in C? Regards, Sri
6
1450
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 until the card number and finish with 9 characters. So it would look like this. Old column 6 19 234 8521 45956 Updated column
3
3101
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 however, because the numbers are rounded (currency), they don't look like they add up right eventhough they do. For instance: if you add $2,632.1743 and $8.6528, you get $2,640.8271. The problem is when the currency rounds these amounts you get:...
60
2679
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 of these values and then average them in another column.
0
8403
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8316
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8833
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8509
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8610
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7345
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6174
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2735
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.