get element position jquery code example

Example 1: how to get element position in jquery

.offset() will return the offset position of an element as a simple object, eg:

var position = $(element).offset(); // position = { left: 42, top: 567 }
You can use this return value to position other elements at the same spot:

$(anotherElement).css(position)

Example 2: jquery get left position

var position = p.position();
var left = position.left; '<-- left value for p

Example 3: Get the position of a div/span tag

function getPos(el) {
    // yay readability
    for (var lx=0, ly=0;
         el != null;
         lx += el.offsetLeft, ly += el.offsetTop, el = el.offsetParent);
    return {x: lx,y: ly};
}

Tags:

Css Example