0

I have some draggable text and I would like to get it's coordinates within an element.

The text is made draggable through this code:

$("span.output").draggable({
    stop: function(event, ui) {}
});

I also have this piece of code which is supposed to make the draggable text the child of the element the draggable text is in:

$('.container').on('drop', function(event, ui){
    $(this).append(ui.draggable);
});

When I try $('#text1').position(); in console when the draggable text is within in the container, I get these coordinates:

Object {top: 57.11805772781372, left: 779.4444580078125}

Unfortunately, these are wrong. Is what I am trying to do even possible? If so, how can I do it?

metersk
  • 11,803
  • 21
  • 63
  • 100
  • Try using `offset()` and check out this question. http://stackoverflow.com/questions/3202008/jquery-difference-between-position-and-offset – James Hay Aug 18 '14 at 23:44
  • `offset()` does not seem to work either. According to the article you linked, wouldn't I want to be using `position()` anyways? – metersk Aug 18 '14 at 23:52
  • What is #text1? Is that an element inside the draggable? – James Hay Aug 18 '14 at 23:56
  • #text1 is a draggable span. It is the draggable element that I put inside the stationary element. Essentially, I want the coordiantes of #text1 within the container, as if the container was the entire webpage...I figured creating a parent-child relationship between the two would allow me to do that. – metersk Aug 18 '14 at 23:59
  • If you're looking for a relative position, you're going to have to determine the position of the parent element and then subtract the resulting position of the dropped element. – DevlshOne Aug 19 '14 at 02:31

1 Answers1

1

This is the problem.

You are using

$('#text1').position();

It will give the postion relative to the container.

If you want the position relative to the document use this.

$('#text1').offset();

It has have $('#text1').offset().left and $('text1').offset().right

It might help..

Ganesh Gaxy
  • 657
  • 5
  • 11
  • May be `offset()` just have two properties `top` and `left` ? It should be `$('#text1').offset().top` and `$('#text1').offset().left` ? – Harry Yu Aug 19 '14 at 05:31