Solved

offsetTop/offsetLeft returning invalid Values

Posted on 2004-10-20
3
1,014 Views
Last Modified: 2010-08-05
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
Comment
Question by:khansoul
  • 2
3 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 12359345

<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
 

Author Comment

by:khansoul
ID: 12359448
Thanks, Please Explain this to me why it works?
0
 
LVL 33

Accepted Solution

by:
knightEknight earned 75 total points
ID: 12359479
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now