offsetParent and offsetTop Question

I am trying to understand how these two functions work.
Lets say I have a Button nested into two Tables I want to get the Position of that Button.

What I do is the following so far


it works fine if there is no nesting, but if it is nested then the Result is not Accurate.

I tried to do something like this..

but still its not correct.

so I want not just to DO this project, I want to UNDERSTAND the logic how should i solve the issue.

Please Explain.(Exact Code is not required)

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

offsetParent refers to an element's container, and offsetLeft/offsetTop are the offsets relative to that container.  If there is no nesting, the container is the window, so you offsetLeft is the same as the element's x position and offsetTop is the element's y position.  Once you nest your elements, however, that won't work.  Here's a simple example:
<div id="outer" style="position: relative; left: 50px; top: 25px;">
  <div id="inner">Hello world</div>

If you alert the offsetLeft of the inner div, you'll get 0, but clearly it's not at the (0, 0) position of the window.  However, inner's offsetLeft + outer's offsetLeft will give you the right answer, because the outer div adds to the inner's overall x-position (relative to the window).  That's the key - so long as the element has a container that isn't the window, you keep adding the offsetLeft.  So a function to do this would look like this:

function getX(obj)
  var temp = obj;
  var left = 0;
  while (temp.offsetParent)
      left += temp.offsetLeft;
      temp = temp.offsetParent;
  return left;

if you now call getX() on the inner div, you'll get the x-position, relative to the screen, of that element.  The getY() function would be analogous, you just replace offsetLeft with offsetTop.  Hope that helps.
my versions of the same functions:

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) );
khansoulAuthor Commented:
> temp = temp.offsetParent;

so if the parameter provided was "this" Then offsetParent will keep on moving to next object?

this = this.offsetParent

Thanks a lot

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

khansoulAuthor Commented:
> knightEknight
You gave me this Solution already to one of my question, but this time i was looking for explaination:)
oh sorry ... I was hasty this time too, I just got here.  I'll read it over.
basically dakyd's functions and mine do the same thing.  Each element exists within the context of its parent element, and each element has an X,Y called offsetTop and offsetLeft.  Our functions sum up these values for each element and its ancenstor elements until there are no more ancestors (parents) and returns the resulting sum.
>> so if the parameter provided was "this" Then offsetParent will keep on moving to next object?

Not sure what you mean by "next object", though I think you've got the right idea.  if you were to actually follow what the value temp holds, it'd look something like this:
iteration1:  temp = this
iteration2:  temp = this.offsetParent
iteration3:  temp = this.offsetParent.offsetParent
iteration4:  temp = this.offsetParent.offsetParent.offsetParent

... and so on and so forth until temp = the object which has no parent itself.  The only element for which that is true is the window.  So basically, you start with your element, and move outward through all the containers until you hit the window.  Along the way, you add up all the offsets (left or top), and the final sum you get is the x or y position relative to the screen.  Hope that helps.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

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.