Hot to get an absolute location of the HTML element

Posted on January 27, 2007 by ZDima.
Categories: Dynamic HTML.

The function below will give you absolute position of an element in the browser window. This you will need to move element to position relative to another element’s position.

function findPos(obj)
{
  var curleft = curtop = 0;
  if (obj.offsetParent)
  {
    curleft = obj.offsetLeft;
    curtop = obj.offsetTop;
    while (obj = obj.offsetParent)
    {
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;
    }
  }
  return [curleft,curtop];
}

Remember, when you need to set new position, the value must be in pixels.

toolTip.style.top = new_Y+"px";
toolTip.style.left = new_X+"px";

no comments yet.

Leave a comment

Names and email addresses are required (email addresses aren't displayed), url's are optional.

Comments may contain the following xhtml tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>