Dynamic width of DIV (offsetWidth issue).

Posted by Tom on Stack Overflow See other posts from Stack Overflow or by Tom
Published on 2009-12-16T09:11:05Z Indexed on 2010/05/01 23:07 UTC
Read the original article Hit count: 261

Filed under:
|

Hi all,
I face issue with retrieving (via javascript) width of the div which content is changed (just before reading the widht via offsetWidth) in dynamic way (via changing innerHTML or using createTextNode).

Here is some sample code:

var con = document.getElementById('avContent'); //content div within page

var temp = document.createElement('div');<br /> 
var text1 = document.createTextNode('CCCCC');<br /> 
temp.appendChild(text1);<br /> 
con.appendChild(temp);<br /> 
var length1 = temp.offsetWidth;<br /> 
var text2 = document.createTextNode('CCCCC33333333vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv');<br /> 
temp.removeChild(text1);<br /> 
temp.appendChild(text2);<br /> 
con.removeChild(temp);<br /> 
con.appendChild(temp);
var length2 = temp.offsetWidth;<br />

The length1 and length2 do have the same width.. (the same result I get while using innerHTML instead of createTextNode).

Looks like it's the same issue like described in following discussion:
http://www.webdeveloper.com/forum/showthread.php?t=187716

Does anybody have answer (work around)?
Thanks much for help in advance.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about html