?
Solved

offsetTop/offsetLeft returning invalid Values

Posted on 2004-10-20
3
Medium Priority
?
1,031 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 300 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
Suggested Courses

752 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