Get div position (top) in javascript?

html, javascript, jquery

Solution

I will give you the vanilla solution.. don't complain.. add a [0] to your element and it's fixed! :P hope this helps.

function getOffset( el ) {
    var _x = 0;
    var _y = 0;
    while( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) {
        _x += el.offsetLeft - el.scrollLeft;
        _y += el.offsetTop - el.scrollTop;
        el = el.offsetParent;
    }
    return { top: _y, left: _x };
}
var x = getOffset( document.getElementById('yourElId') ).left; 

Problem

One div i set the height in css using `top:-26px;`. I have other divs other places where i'd like to align with that div. I notice in jquery writing `.css('top')` gets me my css and not the y coord of the div. How do i get the absolute x,y position using javascript?

Original source