Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Apr 2008
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Variable width table; how to get width?

    I have a table in a div, and I want to get its current width depending on what text is in it:

    Code:
    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:
    Code:
    <!-- 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!
    Last edited by donelson; 05-05-2008 at 11:11 PM.

  • #2
    New Coder
    Join Date
    Apr 2008
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Experts-Exchange.com guru replied:

    Use: toolTip_field.offsetWidth;

    That worked, thanks.

  • #3
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,304
    Thanks
    28
    Thanked 276 Times in 270 Posts
    Quote Originally Posted by donelson View Post
    I have a table in a div, and I want to get its current width depending on what text is in it:

    Code:
    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.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • Users who have thanked Arbitrator for this post:

    donelson (05-06-2008)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •