Kourosh wrote:
Is there a way to get the hight of text in a table using code?
Quite a strange question; this implies that you would do some
positioning in regards of the text size, instead of the containing
elements' size. What if the user resizes the window, therefore
potentially increasing the height of the table? I think that explaining
why you want this done could attract far better suggestions that those
proposed below :)
In IE, you can create a range around the text you want to measure, and
then retrieve the dimensions of the range, through the bounding properties.
In other user agents, you would have to determine the line-height of
your container; if you define it in a stylesheet, then reading it
through the styleSheets object could be effective, provided the user
does not alter it. However if you let it to the browser, then you'd have
to retrieve the float value for the computed CSS value of the
"line-height" property, and to date, I'm aware of no browser fully
implementing the methods that you'd use to do so for this specific
property.
Eventually, you could of course emulate such a process by creating one
cell on the fly, with similar CSS characteristics, containing only one
letter, and then measuring the line-height by removing appropriate insets...
---
<table>
<tr>
<td>Hello, World</td>
</tr>
</table>
<a href="#" onclick="alert(h()); return false">Text height</a>
<script type="text/javascript">
// Range-based, IE only
// We make the assumption that the height is uniform
// within the cell
function h(){
var c, d=document, r, h=-1;
if(
d &&
d.getElementsByTagName &&
d.body &&
d.body.createTextRange
){
c=d.getElementsByTagName("td")[0];
r=d.body.createTextRange();
r.moveToElementText(c);
r.collapse(true);
h=r.boundingHeight;
}
return h;
}
</script>
---