Tim Romano

Navigation

Skip navigation.

Search

Site navigation

Email conversation

FromTim Romano
ToMe
Subjectpossible to get Left position of a relatively positioned Element ?
Date21 May 04 22:31
Hi.
Thanks for the great site!

If I have an anchor element inside a table cell --

<TD ID='cell2'><A ID='anchor99'>my anchor</a></TD>

is there any way to *get* the LEFT value for either element as it is
currently displayed in the browser window?  They're not positioned
absolutely.

Regards
Tim
FromMe
ToTim Romano
SubjectRe: possible to get Left position of a relatively positioned Element ?
Date21 May 2004 23:23
Tim,

yes, this is in fact fairly easy. I have documented how to do this on
http://www.howtocreate.co.uk/tutorials/javascript/browserspecific

You need to call the findPosition function, passing it a reference to the
anchor. You have used an ID, not a name, so I will assume that you want
this to work in all modern browsers.

function findPosition( oLink ) {
  if( oLink.offsetParent ) {
    for(var posX=0,posY=0;oLink.offsetParent;oLink=oLink.offsetParent) {
      posX += oLink.offsetLeft;
      posY += oLink.offsetTop;
    }
    return [ posX, posY ];
  } else {
    return [ oLink.x, oLink.y ];
  }
}

var leftTop = findPosition(document.getElementById('anchor99'));
var leftPos = leftTop[0], topPos = leftTop[1];

Hope this helps

Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/
FromTim Romano
ToMe
SubjectRe: possible to get Left position of a relatively positioned Element ?
Date22 May 04 00:13
Mark,
Beautiful! Thanks so much for the lightning reply. You have given me the
gift of a weekend! Enjoy yours.
Tim
This site was created by Mark "Tarquin" Wilton-Jones.
Don't click this link unless you want to be banned from our site.