Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get element absolute screen position?

Status
Not open for further replies.

wjdunn3

Programmer
Oct 26, 2001
2
US
I need to position a <DIV> next to a table cell (<TD>) when the user mouses over. The table and table cell are NOT absolutely positioned. How can I determine the absolute pixel position of the <TD> element?
I have a reference to the TD element in my code. I have tried el.style.top, etc... but I get 0. Is there a element or style property that will tell me where the element was rendered on the screen?
 
You can try this:

The layerName is the id you give to the element. You can even put this in an alert box to see it.

<Script Language=&quot;Javascript&quot;>
<!--
function PositionEl(layerName)
{
document.getElementById(layerName).style.position;
}
//-->
</Script>

<td id=&quot;layer1&quot;>
blah blah
</td>

Now you just need to figure out how you want to call the function or don't put it in a function at all, it's up to you.
 
Thanks Mithrilhall!
I also figured out that this works:

var srcTop, srcLeft, srcWidth, srcHeight;
var eParent, oSubMenu;
/*get a ref to the event source element*/
if (document.all)
source3=event.srcElement
else if (document.getElementById)
source3=e.target;

/*get abs location of source element*/
srcTop = source3.offsetTop;
srcLeft = source3.offsetLeft;

eParent = source3.offsetParent;
srcTop = srcTop + eParent.offsetTop;
srcLeft = srcLeft + eParent.offsetLeft;

srcHeight = source3.offsetHeight;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top