Forum Moderators: open

Message Too Old, No Replies

IE6 problem with a div width within a table

         

BreatheEasy

2:43 pm on Jan 22, 2004 (gmt 0)

10+ Year Member



Getting a curious problem in IE6 on XP. I require the width of a DIV to position it correctly on a page. I can access 'offsetWidth' of DIV fine outside a table (and also in NS7) but as soon as I put the code into a TD it starts returning zero in IE6.
Due to the design of the site, I cannot avoid putting my HTNL code within a TD element. Any hacks/workarounds/solutions?

For example:-
<div id="box" style="width:200px;">Hello World</div>

<script language="JavaScript">
<!-- //
alert(document.getElementById("box").offsetWidth);
// -->
</script>

<table>
<tr>
<td>
<div id="box2" style="width:200px;">Hello World Again</div>
<script language="JavaScript">
<!-- //
alert(document.getElementById("box2").offsetWidth);
// -->
</script>
</td>
</tr>
</table>

Purple Martin

11:48 pm on Jan 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I haven't tested this idea but...

Maybe it's because you're trying to get the width before the table has finished being defined. Does it work if you move the JavaScript to after the table? Or call it onLoad to make sure rendering is finished?

<table>
<tr>
<td>
<div id="box2" style="width:200px;">Hello World Again</div>
</td>
</tr>
</table>
<script language="JavaScript">
<!-- //
alert(document.getElementById("box2").offsetWidth);
// -->
</script>

DrDoc

3:46 pm on Jan 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to Webmaster World!

Yes, it's one of many IE bugs, and there's really not a whole lot you can do about it. But, in this case you already know the width of the div... So why do you need to use JavaScript to find it out?

BreatheEasy

3:56 pm on Jan 22, 2004 (gmt 0)

10+ Year Member



It's for an interactive map with different city names - each is obviously a different width and I want to align them centrally over a 'dot' to signify the place.

To do this I put the name into the DIV, get the width and divide by two and reposition by this figure. Well, that was the theory anyway!