View Full Version : How Do I Get the Absolute Position of a DIV?

06-28-2009, 06:55 PM
Before you immediately give me an answer, let me describe fully what I'm trying to do, as it's not as simple as the title suggests.

I need to get the absolute position of a DIV (top and left properties) relative to the viewport/screen.

I've tried using .offsetTop and .offsetLeft, but these return the top and left properties relative to the DIV's immediate parent element, not the viewport (unless of course the DIV is top-level and not contained in any other DIVs).

So, is there any way to retrieve the absolute position of a DIV relative to the viewport, even if that DIV is nested in other DIVs?

Thanks for any help!

06-28-2009, 07:58 PM
Go through all the parents adding up all the positions, like so:

var el=document.getElementById('id');
var cl=0,ct=0;
if (el.offsetParent)