Determine the position of an element relative to a positioned ancestor of its offset parent

dom, jquery, offset, position

Solution

You can calculate it based on offset:

var topoffset = $('#target').offset().top - $('#a').offset().top;
var leftoffset = $('#target').offset().left - $('#a').offset().left;

Problem

I want to determine `#target`'s offset relative to `#a` in the following HTML document – i.e., I want to find the values of `x` and `y`: http://dl.dropbox.com/u/2792776/screenshots/2012-06-09_1814.png jQuery has `position()`, but `$("#target").position()` returns `#target`'s offset relative to its offset parent, which is `#c` (not `#a`) I need a function that's equivalent to `$.fn.position()`, but instead returns the position relative to an "offset ancestor" of the target, rather than its direct offset parent. For example: `$("#target").positionRelativeTo("#a")`

Original source