• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1048
  • Last Modified:

offsetTop/offsetLeft returning invalid Values

Hi, somehow offsetTop does not return Correct value. I have this object in the middle and Extreme Right of my page. but it always return "2". why?

<tr bgcolor=#E1E1E1>
       <td align=center >10/5/2004</td><td align=center>Misc</td><td align=center>BENQ</td><td align=center>PB8220</td><td align=center>

---- >>>>       PBW12221<a href="#" onClick="alert(this.offsetTop)">T</a>

so see this line...

<a href="#" onClick="alert(this.offsetTop)">T</a>
always return 2, This <tr> loops and gets records from the database.

Please help.
  • 2
1 Solution

<SCRIPT language="javascript">
function getX(obj)
  return( obj.offsetParent==null ? obj.offsetLeft : obj.offsetLeft+getX(obj.offsetParent) );

function getY(obj)
  return( obj.offsetParent==null ? obj.offsetTop : obj.offsetTop+getY(obj.offsetParent) );

<a href="#" onClick="alert(getX(this)+','+getY(this));return false;">T</a>
khansoulAuthor Commented:
Thanks, Please Explain this to me why it works?
by itself, obj.offsetTop returns the position of this object relative to its parent object -- not relative to the whole page.  The provided functions sum up the successive offsetTop values for the object and each of its ancestor objects, resulting in the offset value of this object relative to the whole page.  :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now