3

I am trying to get the position of a specific div when it is dropped. With some help i have put together the code bellow. I added in the last bit to try and get the specific values but it simply returns [object Object] instead of something like [0,0] or [0,120].

So the question is how do i get the actual values from the array?

Here is a jsFiddle

$(function() {
    $('.AppList').droppable({
        accept: ".App",
        tolerance: 'fit',
        drop: function(event, ui) {
            var apps = $(".App"),
            positions = [];

            $.each(apps, function (index, app) {
                var positionInfo = $(app).position();

                positions.push(positionInfo);
            });
            var Time = positions.slice(0,1);
            var x=document.getElementById("posThis");
            x.innerHTML=Time;
            console.log(positions);
        }
    }); 
});
ChristopherStrydom
  • 5,150
  • 5
  • 17
  • 32

1 Answers1

5

The problem here is that positionInfo is an object, not an array. Time is an array with one such object inside.

I believe you want something like this:

var Time = positions[0];
var x=document.getElementById("posThis");
x.innerHTML= '[' + Time.left + ',' + Time.top + ']';
bfavaretto
  • 69,385
  • 15
  • 102
  • 145
  • Actually, `Time` is an array with one element, so the default string representation of the array will be equivalent to the one of the first element. Which is an object. Good catch though! (if there were two elements, OP would have seen something like `[object Object], [object Object]`). – Felix Kling Apr 26 '13 at 21:04
  • Glad to help, @ChristopherStrydom! – bfavaretto Apr 26 '13 at 21:10
  • I'm going to be cheeky and ask another question... how would i go about saving the positions with a cookie and retrieving them on a page refresh? So all the divs return to their saved positions. Or should i leave that for another question? – ChristopherStrydom Apr 26 '13 at 21:12
  • @ChristopherStrydom I believe that's more suited for another question, as I wouldn't have enough room to answer in comments. Just some pointers: one way to do that would be passing the values to the server (using ajax), and creating the cookies there; if you want to create cookies client-side, look into `document.cookie`. – bfavaretto Apr 26 '13 at 21:15
  • Okay thanks for the tips but i don't have a clue how to do that :/ I wish this 'forum' had a way of requesting specific people to answer a question. Anyway you will probably see a new question popping up soon. Thanks again – ChristopherStrydom Apr 26 '13 at 21:20
  • 1
    I probably won't be the one answering that, as I don't know a lot about cookies. This may help: http://stackoverflow.com/questions/1458724/how-to-set-unset-cookie-with-jquery – bfavaretto Apr 26 '13 at 21:27
  • Yea that is very handy! Once again a great help! I have added my extended question to the bottom of this one btw EDIT: i am going to remove the extended question till i have tried some of my own code.. i have already got a -1 for it :L – ChristopherStrydom Apr 26 '13 at 21:50