Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old April 17th, 2007, 07:46 AM
Registered User
 
Join Date: Mar 2007
Location: BOONEVILLE, MS, USA.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default adding column numbers

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.....

function sumup() {
 document.getElementById("Table1")
  column = document.getElementsByTagName("colFour");
  document.myForm.tbTotal.value = column;
}
.....
<TABLE NAME="Table1" id="Table1">
<COL WIDTH=10*>
    <COL WIDTH=139*>
    <COL WIDTH=27*>
    <COL WIDTH=29*>
    <COL WIDTH=27* NAME="colFour" id="colFour">
    <COL WIDTH=10*>
.....
<INPUT TYPE=TEXT NAME="tbTotal">
<INPUT TYPE=BUTTON NAME="pbTest" VALUE="TEST" onclick="sumup();"./>
.....But when I click pbTest, the result in tbTotal is.....
[object]
.....What am I doing wrong?



decren
  #2 (permalink)  
Old April 17th, 2007, 09:27 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

getElementsByTagName retrieves elements by tag name, your table will have rows of tr with cells of td. You need something like:
Code:
var oTable = document.getElementById("Table1");
var colRows = oTable.rows;
var dTotal = 0.0;
for (var i = 0; i < colRows.length; i++)
{
  var oRow = colRows[i];
  var oCell = oRow.cells[3];
  dTotal += //get data from cell, you haven't shown how it's held...
}
alert(dTotal);
--

Joe (Microsoft MVP - XML)
  #3 (permalink)  
Old May 5th, 2007, 07:48 AM
Registered User
 
Join Date: Mar 2007
Location: BOONEVILLE, MS, USA.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you joefawcett for the code. I played around with it a little but couldn't get it tweaked just right. But you did give me some ideas which I used to tweak a table I found at:
http://4umi.com/web/javascript
Again, thanx for the input.

decren


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding numbers in #.###,0 format margreta XSLT 6 April 25th, 2006 09:52 AM
Adding numbers Hudson40 Excel VBA 3 November 23rd, 2005 03:02 AM
adding numbers gmoney060 Access 1 August 15th, 2004 08:14 PM
Adding numbers kend XSLT 4 June 27th, 2003 12:33 AM
Adding numbers kend XSLT 1 June 26th, 2003 10:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.