Need to calculate offsetRight in javascript

Posted by Dan Bailiff on Stack Overflow See other posts from Stack Overflow or by Dan Bailiff
Published on 2010-06-08T20:09:29Z Indexed on 2010/06/08 20:12 UTC
Read the original article Hit count: 396

Filed under:
|

I need to calculate the offsetRight of a DOM object. I already have some rather simple code for getting the offsetLeft, but there is no javascript offsetRight property. If I add the offsetLeft and offsetWidth, will that work? Or is there a better way?

function getOffsetLeft(obj)
{
    if(obj == null)
        return 0;
    var offsetLeft = 0;
    var tmp = obj;
    while(tmp != null)
    {
        offsetLeft += tmp.offsetLeft;
        tmp = tmp.offsetParent;
    }
    return offsetLeft;
}

function getOffsetRight(obj)
{
    if (obj == null)
        return 0;
    var offsetRight = 0;
    var tmp = obj;
    while (tmp != null)
    {
        offsetRight += tmp.offsetLeft + tmp.offsetWidth;
        tmp = tmp.offsetParent;
    }
    return offsetRight;    
}

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about offset