View Full Version : Height

05-30-2006, 12:18 AM
Hi, i am trying to find the width of characters in an element like so but it's coming up blank

function check(){
var val=document.getElementById('mydiv').style.width;

<body onload="check()">
<div id="mydiv">I am a div</div>

I know that if i add style="width:100%" to the div then the function would return 100% but say i wanted to find how much width the above paragraph actually took up (ie looks about 40px), is this possible :confused:

05-30-2006, 04:37 AM
If you don't set the size expicitly on a block level element
it will take up as much space as there is in the window.
But you'll only get a property for style.width if you set the style width inline.

This code will give you the size of the element passed as an argument-
It returns an array of two integers- [width,height];

function elSize(hoo){
return Math.max(hoo.scrollWidth,hoo.offsetWidth),

05-30-2006, 12:33 PM
That would give you the width of the element, not how much room the characters took up.

I managed to figure it out without setting the width inline

document.defaultView.getComputedStyle(docObj, "").getPropertyValue(property)

05-30-2006, 12:57 PM
Interesting. Where did you find that?

05-30-2006, 02:57 PM
mr know it all... Google :thumbsup:

05-30-2006, 08:19 PM
one of my standard functions

function zxcGetStyle(zxcel,zxcs){
if (zxcel.currentStyle){ return zxcel.currentStyle[zxcs]; }
if (window.getComputedStyle){ return document.defaultView.getComputedStyle(zxcel,null).getPropertyValue(zxcs); }
return false;

modified from QirksMode