SivaRajini SivaRajini - 2 years ago 499
CSS Question

how to get bounding box for div element in jquery

i want to calculate bounding box for div element via jquery/javascript.

i tried like this .

//Left side of box
//Top side of box
//Right side of box
document.getElementById("myElement").offsetLeft + document.getElementById("myElement").offsetWidth;
//Bottom side of box
document.getElementById("myElement").offsetTop + document.getElementById("myElement").offsetHeight;

it returns some values. whether it is correct way to get bounding box for div element via
jquery / javascript

i need something like
method in
element. it will return x,y ,width and height of an element. sameway how can i get bounding box for div element.



Answer Source

You can get the bounding box of any element by calling getBoundingClientRect

var rect = document.getElementById("myElement").getBoundingClientRect();

That will return an object with left, top, width and height fields.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download