...

View Full Version : Variable width table; how to get width?



donelson
05-05-2008, 11:08 PM
I have a table in a div, and I want to get its current width depending on what text is in it:


var toolTip_field = document.getElementById("toolTip_field");

// This displays the correct text string on screen
toolTip_field.innerHTML = "This is a string";

alert(toolTip_field.width) --> blank alert box

My toolTip field is defined as:

<!-- Tool Tip code -->
<div id="toolTip_obj" style="position: absolute; left: 600px; top: 300px; z-index:4;">
<font size="1" face="Arial, Helvetica, sans-serif">
<table border="1" cellpadding="2" cellspacing="0">
<tr>
<td id="toolTip_field" valign="top" align="center" bgcolor="#FFFFcc">
tool tip
</td>
</tr>
</table></font>
</div>

Is it possible to get the field's width?

Thanks!

donelson
05-05-2008, 11:43 PM
Experts-Exchange.com guru replied:

Use: toolTip_field.offsetWidth;

That worked, thanks.

Arbitrator
05-06-2008, 02:56 AM
I have a table in a div, and I want to get its current width depending on what text is in it:


alert(toolTip_field.width) --> blank alert box

Is it possible to get the field's width?

If you were looking for the CSS width, checking a non‐existent width attribute on the td element (as in your code above) isnít the way to do it. The standard‐based way to do it is to use getComputedStyle and the Internet Explorer way is currentStyle. Then thereís also the offsetWidth property that you found, of course.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum