views:

59

answers:

3

How do I find the width of a table or columns in a table without specifying it in HTML or Javascript?

For example:

<table id="myTable">
<tr><td>column a</td><td>column b</td></tr>
</table>

In Javascript:

var tbl = document.getElementByID("myTable");
alert(tbl.style.width);  //will be undefined
A: 

It's dangerous to attempt these things in raw DOM calls because browsers differ.

The safe thing is to use a library (like jQuery) and use their routines.

To do exactly your example in jQuery:

 $("#myTable").width()
Jason Cohen
A: 

You could try:

var tbl = document.getElementByID("myTable");
alert(tbl.clientWidth);

It work's fine in FireFox, but you should check it in IE.

Hippo
+1  A: 

There is no direct correspondense between CSS style keywords and javascript keywords. In your case, you need tbl.offsetWidth, not tbl.style.width. See this quirksmode article for starters.

buti-oxa
I like that link.
ssorrrell