[Javascript] Get HTML element size
Richard Lewis
richardlewis at fastmail.co.uk
Tue Mar 8 04:07:11 CST 2005
On Tue, 8 Mar 2005 10:41:29 +0100, "Simone Fumagalli" <simone at tomato.it>
said:
> Ciao javascript,
>
> How do I get the width and the height of a element in my page ?
>
> Suppose I have this HTML:
>
> <div id="mydiv">
> <p>Hello people !!</p>
> </di>
>
> with this JS code :
>
> my_element = getElementById("mydiv");
> alert (my_element.style.width)
>
note that getElementById() by itself shouldn't work; you should have
document.getElementById().
> I get an empty string :-(
>
> Suggestions ?
>
I think that the style properties retrievable via JavaScript are only
those which have been explicitly set:
given:
<div id="mydiv" style="top:5px;left:5px;width:100px;height:20px">
<p>........</p>
</div>
the script:
my_element = document.getElementById("mydiv");
alert (my_element.style.width)
will return the string "100px".
Note that you should also be able to set style properties using
JavaScript:
e.style.width="200px";
Cheers,
Richard
More information about the Javascript
mailing list