Solved

offsetTop/offsetLeft returning invalid Values

Posted on 2004-10-20
3
1,019 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Auto refresh part of an MVC5 View 7 61
Animated .jpg? 13 61
Message not shown 5 33
jquery to restrict certain words from input in form 11 17
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

867 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

22 Experts available now in Live!

Get 1:1 Help Now