PDA

View Full Version : get width of a div that changes dynamically


leftpeg
08-21-2006, 10:26 AM
I have a div whose width depends on the length of the longest string I put into it using innerHTML. so my div contents look like

a_string
a_longer_string
the_longest_string
short_string

I would like to get the width of this div. How can I do this?

document.getElementById("myDiv").style.width returns nothing.


Thanks.

Kor
08-21-2006, 11:23 AM
document.getElementById("myDiv").style.width returns nothing, because you have no style and no width. Try the readonly offset attributes

document.getElementById("myDiv").offsetWidth

Important note:the return value is decimal number
--------
On the other hand... hm... If no width declared, the div (as it is a block-level element) will always take the width of it's parent, thus I can not see how your div can "expand/contract" depending on it's childs' width.

Apostropartheid
10-31-2007, 06:52 PM
If you float a div without a width set, it'll take the width of its child :)