0

I have an html page with two div elements. One is on the complete left of the page and it has classname toolbox. Another is just on the right of toolbox and it has classname canvas. Inside toolbox, there is a square shaped element with classname node1. Its width and height are 50 pixels each. The following code makes it possible to drag the node1 from toolbox to canvas. As you can see, there is a variable named x which is a clone of node1. Just after node.helper.remove() I want to write code to find the coordinates of the left top of the clone. The coordinates should be with respect to the canvas. Could you tell me what code to write?

$(document).ready(function(){
$(".node1").draggable({
    helper: 'clone',    // cloning the node/icon.
    cursor: 'move'      // move with cursor.
})

$(".canvas").droppable({

    drop: function(event, node) 
    {

        var x = node.helper.clone();

        x.draggable({
                containment: '.canvas'
                    });

        node.helper.remove();   
    }
});
});
1

2 Answers 2

2

You can use .offset() which is relative to the document and .position which is relevant to the direct parent.

.offset() returns an object with the x and y coordinates of the referenced object. You can access them like this:

$('Element').offset().top;
$('Element').offset().left;
Sign up to request clarification or add additional context in comments.

Comments

0
$(".node1").draggable({
    helper: 'clone',
    cursor: 'move',
});

$(".canvas").droppable({
    drop: function (event, node) {
        var x = $(node.helper).clone();
        $(this).append(x);
        console.log($(x).position()); //Returns top/left Coordinates
    }
});

2 Comments

Thank you. That gives me coordinates but here we are appending x. After getting coordinates, is there a way to remove it? How to do that? I tried $(this).remove(x) but it did not work.
$(x).remove() works. So the problem is solved. How do I accept your answer? I do not see any option for that here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.