Before I begin, I’ve only tested this using Firefox on a Mac, but it should work in any browser…
I’ve seen a lot of people trying to find out the height of a table cell, and I have on many occasions have tried myself only to give up. You can get the height of a cell, but it’s always going to be the height you set it to, not the height it becomes after data has been placed into it and it expands. What a pain, right? Well, I’ve found a solution to this problem, though it involves a few DIVs and javascript.
So, say you have this:
|
Instead of that, change it to:
|
See where I’m going? We have pretty much created two points to measure by… the content_top DIV and the content_bottom DIV. Now, how do we measure them?
Whenever we need the height, we call a function, say getTDHeight(). getTDHeight looks something like this:
|
So basically we are asking a function called getPixelsFromTop to give us the number of pixels the element “content_top” is from the top of the browser window. The same for the element “content_bottom”. With this, we’ll have something like content_top = 100 and content_bottom = 350. So we take the top value away from the bottom and that leaves us with the height of the cell… 250px
The getPixelsFromTop function is no biggie either:
|
All it does is constantly adds the objects location from the top of it’s parent container, then loops up to that parent and repeats until it hits the top of the DOM, then returns the value.
Hope this helps. I’ve seen a lot of frustration over this on many forums. I know it’s a pain, but at least it is possible.
April 23, 2007 at 11:26 am |
nice tips…but in getTDHeight() is “contentHeight = contentBottom – contentTop;” instead of “contentHeight = contentBottom – contentHeight;”. And i use mozilla firefox in windows, and i have add the var declarations for each variables
September 12, 2007 at 6:49 am |
Very good trick. Nice from your part to share it with us.
July 2, 2008 at 9:02 am |
it would seem this would only return the height if it were the tallest cell in the table
September 5, 2008 at 9:44 am |
Federico is right;
This line is wrong:
contentHeight = contentBottom – contentHeight;
If should say this instead:
contentHeight = contentBottom – contentTop;
I’ve tried this script on IE 7, FireFox and Google Chrome on PC, and it works on all.
Thanks
September 24, 2008 at 3:51 pm |
Thanks alot..it really helped..I had to stretch an absolute block in a td and IE 6 didn’t like the top:0 and bottom:0..it only likes the first one..so thanks to your script, I managed to modify the height of the block (the only way to stretch it) according to the td height!! It worked like a charm!!
thanks again
September 26, 2008 at 8:32 am |
Hi m8,
I managed to get it working, thank u v much indeed! whats the deal with firefix though, do u have a working version for this also?
January 30, 2009 at 5:52 pm |
Nice workaround. However, this seems to work just fine in IE6+ but am unable to get Firefox3 to respond (no errors either). What’s the last version of Firefox this was tested in?
January 30, 2009 at 5:56 pm |
DOH! Sorry…I was trying to take this one step further and explicitly set TD heights (based on the results from your workaround above). Your workaround results work fine – but this explicit setting of TD heights in Firefox is not (my mixup).
March 2, 2009 at 5:29 pm |
It’s not “height’s”, it’s “heights”
May 14, 2009 at 6:05 am |
Thanks for the code.
August 12, 2009 at 4:22 am |
Your code really help me, Thanks a lot
September 8, 2009 at 5:27 pm |
Why don’t you use
document.getElementById(theID).offsetHeight