Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Obtain table width after the fact.

Posted on 2007-09-27
8
Medium Priority
?
2,230 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!
0
Comment
Question by:jglazer63
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 3

Expert Comment

by:rlbalan
ID: 19975373
 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

0
 

Author Comment

by:jglazer63
ID: 19975379
Nope.  Width comes back as ""  (blank).  Already thought of that.
0
 
LVL 19

Expert Comment

by:dakyd
ID: 19975391
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:jglazer63
ID: 19975406
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.
0
 
LVL 19

Accepted Solution

by:
dakyd earned 1000 total points
ID: 19975448
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.getElementById('theTable'));" />
</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.
0
 
LVL 3

Expert Comment

by:rlbalan
ID: 19975495
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
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20432410
Forced accept.

Computer101
EE Admin
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question