Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

clientWidth always returning 0 1

Status
Not open for further replies.

shu745

Programmer
May 14, 2001
79
US
Hi All,

I'm trying to get the width of each column of a table. But when I use the clientWidth property, it always returns 0. Why is this and is there a way to fix it or ANOTHER way to find out the width (not using style.width) of a column?

Much thanks!
 
Is the table visible? It will always return 0 if display is set to "none".

Adam
while(ignorance==true){perpetuate(violence,fear,hatred);life=life-1};
 
I thought clientWidth only applied to the browser window?!

Try something like this to get the width of the first column of the first table. I don't know if the method is accurate but hope it helps :)

document.getElementsByTagName('TABLE')[0].colls[0].style.width

----------
I'm willing to trade custom scripts for... [see profile]
 

Shu,

This works in IE, NN, Mozilla, and Opera, and, as asked, does not use 'style.width':

Code:
<html>
<head>
<script language=&quot;javascript&quot;>
<!--
	function findColumnWidths(tableId)
	{
		var myTable = document.getElementById(tableId);
		var outputStr = '';
		for (var colLoop=0; colLoop<myTable.rows[0].cells.length; colLoop++) outputStr += 'Column ' + (colLoop+1) + ' width: ' + myTable.rows[0].cells[colLoop].offsetWidth + '\n';
		alert(outputStr);
	}
//-->
</script>

</head>
<body>

<table id=&quot;myTable&quot; width=&quot;75%&quot; cellspacing=&quot;2&quot; border=&quot;1&quot;>
<tr>
	<td>abcdefg</td>
	<td>hij</td>
	<td>klmnopqrstuvwxyz</td>
	<td>1</td>
	<td>23456</td>
	<td>789</td>
</tr>
</table>

<br>Click <a href=&quot;javascript:findColumnWidths('myTable');&quot;>here</a> to find the column widths of the first row

</body>
</html>

Unlike style.width, offsetWidth does not require you to set its value before it can be read.

Hope this helps!

Dan
 
Top piece of code Dan. I'll save that to my &quot;snippets&quot; directory and give you a star! [thumbsup2]

Pete.


Web Developer / Aptrix CMS (LWWCM) Specialist
w: e: Pete.Raleigh(at)lclimited.co.uk
 
Anyone know of a way to get the width of a column on the fly? I am thinking this isn't possible. The code in this thread works great once the page is loaded but it gives me a zero when implemented on the fly.
 
Well, since many tables adjust their column widths to fit the largest cell's contents, it might not make sense to try and grab the column width until the table is finished drawing (even if the page isn't finished).

So what you might consider is putting a call to the findColumnWidths(...) function after the close-TABLE tag (</TABLE>), in a standalone script:

</tr>
</table>
<script>
var w = findColumnWidths('myTable');
</script>
...

Of course, you'll have to adjust findColumnWidths(...) here to return a value and have that value be the column width in which you're interested.

Is that enough information? Provide some more details about your specific situation if it isn't.

--Dave
 
I win the dumb guy award for the day. I was getting a zero on the fly because the size of the tbale and its columns were dependent on the parent table and I was calling the function to get the size before the end tag for THAT table. Thanks for pointing me down the right track LookingForInfo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top