• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1042
  • 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>
             
            </td>
       </tr>



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.
0
khansoul
Asked:
khansoul
  • 2
1 Solution
 
knightEknightCommented:

<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) );
}
</SCRIPT>


<a href="#" onClick="alert(getX(this)+','+getY(this));return false;">T</a>
0
 
khansoulAuthor Commented:
Thanks, Please Explain this to me why it works?
0
 
knightEknightCommented:
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.  :)
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

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