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

How do I sum columns in TBODY?

P: n/a
Ok, I have my data in a tbody

I want the last row of my table to sum the columns

ColA ColB ColC
<tbody id=mybod>
1 2 3
2 3 5
</tbody>
3 5 8

Is there a javascript code for this?

thanks!

Jan 1 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
lo*****@gmail.com said the following on 1/1/2006 12:08 PM:
Ok, I have my data in a tbody
Put it in TD's
I want the last row of my table to sum the columns
OK
ColA ColB ColC
<tbody id=mybod>
1 2 3
2 3 5
</tbody>
3 5 8

Is there a javascript code for this?
Yes.
thanks!


Welcome.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 1 '06 #2

P: n/a
Ok, i want it to sum automatically. May i ask for the Javascript code?

Jan 1 '06 #3

P: n/a
lo*****@gmail.com said the following on 1/1/2006 12:15 PM:

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.
Ok, i want it to sum automatically. May i ask for the Javascript code?


Post a sample of your initial effort. Putting your data in a TBODY is
not the best way to accomplish it. Place it in a TD element with ID's
that correspond to the columns.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 1 '06 #4

P: n/a
Ivo
"Randy Webb" wrote
lo*****@gmail.com said
Ok, i want it to sum automatically. May i ask for the Javascript code?


Post a sample of your initial effort. Putting your data in a TBODY is
not the best way to accomplish it. Place it in a TD element with ID's
that correspond to the columns.


Yes, a TBODY element only takes TR elements as its children, which take only
TD (and TH) as children. They do accept your raw text as child.
But giving every TD an ID is wasteful. Once you have a reference to the
TABLE or TBODY element, you can walk through its childnodes and collect the
data from the TD's.
A quick and simple example, catering for different amounts of cells per row
but not for any additional markup in the TD's:

function sumup() {
var t = [], c, d = document.getElementById( 'sumtable' ),
r = d.rows, i = r.length - 1, lastrow = r[i];
while(i--) { c = r[i].cells; j = c.length;
while(j--) { if( !t[j] ){ t[j] = 0; }
t[j] += parseFloat( c[j].firstChild.nodeValue );
}
}
j = t.length; while(j--) {
if( lastrow.cells[j] ) { lastrow.cells[j].firstChild.nodeValue = t[j]; }
}
}

See it in action at
< http://4umi.com/web/javascript/tablesum.htm >

--
hth
ivo
http://4umi.com/web/javascript/
Jan 1 '06 #5

P: n/a
Ivo said the following on 1/1/2006 3:03 PM:
"Randy Webb" wrote
lo*****@gmail.com said
Ok, i want it to sum automatically. May i ask for the Javascript code?


Post a sample of your initial effort. Putting your data in a TBODY is
not the best way to accomplish it. Place it in a TD element with ID's
that correspond to the columns.

Yes, a TBODY element only takes TR elements as its children, which take only
TD (and TH) as children. They do accept your raw text as child.
But giving every TD an ID is wasteful. Once you have a reference to the
TABLE or TBODY element, you can walk through its childnodes and collect the
data from the TD's.


True. But my suggestions were not to give a full-blown answer but rather
to make the OP think and try to figure it out instead of giving a
ready-made answer.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 2 '06 #6

P: n/a
Randy Webb <Hi************@aol.com> wrote in
news:B8********************@comcast.com:
Ivo said the following on 1/1/2006 3:03 PM:
"Randy Webb" wrote
lo*****@gmail.com said

Ok, i want it to sum automatically. May i ask for the Javascript
code?

Post a sample of your initial effort. Putting your data in a TBODY is
not the best way to accomplish it. Place it in a TD element with ID's
that correspond to the columns.

Yes, a TBODY element only takes TR elements as its children, which
take only TD (and TH) as children. They do accept your raw text as
child. But giving every TD an ID is wasteful. Once you have a
reference to the TABLE or TBODY element, you can walk through its
childnodes and collect the data from the TD's.


True. But my suggestions were not to give a full-blown answer but
rather to make the OP think and try to figure it out instead of giving
a ready-made answer.


It rather did look like a request for getting an answer to a school
assignment, didn't it?

Jan 2 '06 #7

P: n/a
VK

Patient Guy wrote:
It rather did look like a request for getting an answer to a school
assignment, didn't it?


You know that, he knows that - but *they* know how to play on people's
vanity like on the piano, do they? :-)

Jan 2 '06 #8

P: n/a
Ivo wrote:
[snip]
Once you have a reference to the
TABLE or TBODY element, you can walk through its childnodes and collect the
data from the TD's. [snip] See it in action at
< http://4umi.com/web/javascript/tablesum.htm >


OT: Ivo, it would have more semantic value to sum all cells in the
tbody|tbodies and then to place that sum into the tfoot, rather than
placing the sum in the last tr of the tbody.

Jan 3 '06 #9

P: n/a
Ivo
"Jambalaya" <ch*********@gmail.com> wrote
Ivo wrote:
[snip]
Once you have a reference to the
TABLE or TBODY element, you can walk through its childnodes and collect the data from the TD's.

[snip]
See it in action at
< http://4umi.com/web/javascript/tablesum.htm >


OT: Ivo, it would have more semantic value to sum all cells in the
tbody|tbodies and then to place that sum into the tfoot, rather than
placing the sum in the last tr of the tbody.


True, but that would complicate the function, having to retrieve a tbody as
well as a tfoot node, and I was aiming something simple and straightforward.
Moreover, the row starts its life with empty cells, how much sematic value
do you see in that?

--
hth
ivo
http://4umi.com/web/javascript/
Jan 3 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.