Get id of draggable item

Is there a way in the original HTML5 drag and drop to get the id of an element with an attribute draggable="true"?

At first, I thought it was standard that you get the identifier from the draggable container, but in fact it is always the identifier from the children element that you start to drag, which is very annoying.

Here is the fiddle: https://jsfiddle.net/chickberger/2ujhodeh/2/

I need the id from div ('correctid') instead of image.

Any help is appreciated. :)

+4
source share
2 answers

, currentTarget target event.

event.target , .

currentTarget, , ( ondragstart).

function drag(ev) {
    ev.dataTransfer.setData("text", ev.currentTarget.id);
    alert(ev.currentTarget.id);
}
+6

"ev.target" - . , parentNode:

function drag(ev) {
    ev.dataTransfer.setData("text", ev.target.parentNode.id);
    alert(ev.target.parentNode.id);
}
0

Source: https://habr.com/ru/post/1629079/


All Articles