Get Id Of Draggable Element
Is there a way in native HTML5 drag and drop to get the id of the element with the attribut draggable='true' ? At first i thought it's standard that you get the id from the draggab
Solution 1:
You can get a reference to that element by accessing the currentTarget
property instead of the target
property on the event
object.
In your case, event.target
was referring to the innermost element that was being targeted.
You want the currentTarget
property since it refers to the element that the event listener is attached to (which would be the element with the ondragstart
attribute).
function drag(ev) {
ev.dataTransfer.setData("text", ev.currentTarget.id);
alert(ev.currentTarget.id);
}
Solution 2:
The 'ev.target' is an image. You can fix this by using the parentNode:
function drag(ev) {
ev.dataTransfer.setData("text", ev.target.parentNode.id);
alert(ev.target.parentNode.id);
}
Post a Comment for "Get Id Of Draggable Element"