We help IT Professionals succeed at work.

Obtain table width after the fact.

jglazer63
jglazer63 asked
on
2,258 Views
Last Modified: 2012-08-13
In javascript, how can I determine the width of a table that is sized based on its contents.  That is, there is no width= specification on the table, it just gets wide enough depending on the content within it.  I need to know how wide it ended up being.

Thanks!
Comment
Watch Question

Commented:
 var w = document.getElementById("myTable").width;
should give the width of the table. provided you defined the table as

<table id="mytable" ...>

Checkout  http://msdn2.microsoft.com/en-us/library/ms535901.aspx
for more info on all the attributes available

Author

Commented:
Nope.  Width comes back as ""  (blank).  Already thought of that.

Commented:
You can use the table's offsetWidth property.  All you have to do is get a reference to that table, and you can get it back out.  For example:

<html>
<head>
<script type="text/javascript">
function getWidth(obj)
{
  alert(obj.offsetWidth);
}
</script>
<style type="text/css">
#theTable {
  border-collapse: collapse;
}

#theTable td {
  border: 1px solid black;
}
</style>
</head>

<body>
<table id="theTable">
  <tr>
    <td>hello</td>
    <td>world</td>
  </tr>
</table>

<input type="button" value="Get table width" onclick="getWidth(document.getElementById('theTable'));" />
</body>
</html>

I used getElementById(), but just about anything will work there.  So long as you pass in the table, the getWidth() function would give you that object's width.

I used a little bit of styling to make the table more visible, but you can safely ignore it.  It doesn't do/add anything to the snippet.  Hope that helps.

Author

Commented:
Is the CSS necessary? I did offsetWidth earlier and it comes back as zero (0) even though the content is there and the table is obviously wider than zero.
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Commented:
My fauilt .. It shud have been offsetWidth

From MSDN:
 offsetWidth : Retrieves the width of the object relative to the layout or coordinate parent, as specified by the offsetParent property
Forced accept.

Computer101
EE Admin

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.