ssorrrell
ssorrrell

Reputation: 679

How to find the width of tables or cells?

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

Upvotes: 1

Views: 149

Answers (3)

buti-oxa
buti-oxa

Reputation: 11431

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.

Upvotes: 1

TheHippo
TheHippo

Reputation: 63159

You could try:

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

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

Upvotes: 0

Jason Cohen
Jason Cohen

Reputation: 83051

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()

Upvotes: 0

Related Questions