Search code examples
javascriptjquerydrag-and-dropposition

Finding absolute position after a div was dropped into a new div?


The question almost says it all...

So. im using jQuery's drag and drop stuff, and I have the following problem.

I have a div #one and it can be dragged and then dropped into the div #two. Evereything works okay for now.

The thing is, that I want to save the position, where the #one was dropped. I get the position of #one with .position() - after dropping, I call .position() and the problem is that it calculates it's position relative to it's parent div (wrapper).

I want the script to calculate the position of #one relative to the #two (div where I dropped #one).

I cheated a bit, created #one immidiately in #two, and set it's left and top properties to -120px and 0px, so that the div would show outside #two, and when I dragged then #one into #two, I would get proper position.. But I don't want to do this like that..

Is there a way to find absolute position on #one relative to the #two after it was dropped in it ?


Solution

  • If I understood correctly, You want to find position (left,top) of #one w.r.t #two. If so, you can use .offset function to find the position of #two and #one and subtract their position to get the relative position of the #one inside #two.

    DEMO here