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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get DIV's true size

Status
Not open for further replies.

NickHowell

Programmer
Jun 28, 2002
4
US
Here's my problem, I have a program where a user can enter text into a <DIV> using the div's .innerText through a textbox. This div uses absolute positioning and is given XYWH dimensions when it is loaded. I need to know the true width/height of the div but the problem is the .style.(dimension) or .style.pos(dimension) do not reflect the actual dimensions of the div when it is expanded to accomadate innerText. Is there some sort of style element which can tell me this information? Thanks
 
Use offsetWidth/offsetHeight (it is a property of the object, not of the style object).

Hope that helps!

Happy coding! :)
 
You can use these functions:

function elemHeight(elemName) {

if (document.layers)
h = eval(&quot;document.layers[&quot; + elemName + &quot;].document.height&quot;)

else if (document.getElementById)
h = eval(&quot;document.getElementById('&quot; + elemName + &quot;').offsetHeight&quot;)

return(h);
}

function elemWidth(elemName) {

if (document.layers)
w = eval(&quot;document.layers[&quot; + elemName + &quot;].document.width&quot;)

else if (document.getElementById)
w = eval(&quot;document.getElementById('&quot; + elemName + &quot;').offsetWidth&quot;)

return(w);
}


function elemTop(elemName) {

if (document.layers)
t = eval(&quot;document.layers[&quot; + elemName + &quot;].document.pageY&quot;)

else if (document.getElementById)
t = eval(&quot;document.getElementById('&quot; + elemName + &quot;').offsetTop&quot;)

return(t);
}


function elemLeft(elemName) {

if (document.layers)
l = eval(&quot;document.layers[&quot; + elemName + &quot;].document.pageX&quot;)

else if (document.getElementById)
l = eval(&quot;document.getElementById('&quot; + elemName + &quot;').offsetLeft&quot;)

return(l);
}


function elemProps(elemName) {

w = &quot;width= &quot; + elemWidth(elemName);
h = &quot;height= &quot; + elemHeight(elemName);
t = &quot;top= &quot; + elemTop(elemName);
l = &quot;left= &quot; + elemLeft(elemName);

dims = &quot;element name: &quot; + elemName + &quot;\n\n&quot; + w + &quot;; &quot; + h + &quot;\n&quot; + t + &quot;; &quot; + l ;

return(dims)
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top