Nope. Width comes back as "" (blank). Already thought of that.
Main Topics
Browse All TopicsIn 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!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
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
</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.
No, the CSS shouldn't be necessary. For example, this worked just fine for me:
<html>
<head>
<script type="text/javascript">
function getWidth(obj)
{
alert(obj.offsetWidth);
}
</script>
</head>
<body>
<table id="theTable">
<tr>
<td>hello</td>
<td>world</td>
</tr>
</table>
<input type="button" value="Get table width" onclick="getWidth(document
</body>
</html>
Mind you, I don't have Firefox on this machine, so I didn't test it on that. offsetWidth is usually pretty cross-browser, though, so I'd be surprised if it didn't work on other browsers.
Business Accounts
Answer for Membership
by: rlbalanPosted on 2007-09-27 at 15:57:13ID: 19975373
var w = document.getElementById("m yTable").w idth;
/en-us/lib rary/ms535 901.aspx
should give the width of the table. provided you defined the table as
<table id="mytable" ...>
Checkout http://msdn2.microsoft.com
for more info on all the attributes available