0

All,

I'm trying to use HTML5 drag and drop and with javascript. However, I can't get the ability to drop an item into the same location of where the mouse gets released. Anytime I drop an item into the destinationContainer the X and Y coordinates don't work for top and left styles respectively.

I have included a jsfiddle for reference. In addition, I have included the snippet code that represents my drop event.

Thanks for any help!

https://jsfiddle.net/mdevera/94vzuo89/2/

function drop(event) {

console.log("drop");

event.preventDefault();

var id = event.dataTransfer.getData("text/plain");

if ((id === "item1" || id === "item2") && event.target.className === "destinationContainer" ) {

    var clone = document.getElementById(id).cloneNode(true);

    clone.id = Math.floor((Math.random() * 100) + 1);

    /// Style for making item drop in the same mouse location of drop. Does not work.
    //clone.style.top = event.clientY + "px";
    //clone.style.left = event.clientX + "px";


    clone.style.position = "relative";
    event.target.appendChild(clone);

}

}

1 Answer 1

1

You can try to get X or Y position using element.offset().top and element.offset().left using jQuery.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Pradip, I'm trying to make this happen without jQuery.

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.