I have been facing a problem when I try to implement SetDragImage on a 'LI' element, the 'LI' has been created dynamically searching inside of a folder and getting the name of the files inside and after being created I set the .draggable() properties
I am able to do drag and drop and the image shown while dragging is the text so I want to change it and show a custom image. The id of each 'LI' contains the source of the image (path + fileName)
I am trying to use SetDragImage but every time I get the error "Uncaught TypeError: Cannot read property 'SetDragImage' of undefined
I have also tried to do it without "originalEvent" and I get the same error
Could someone help me to find my mistake?
Please find below my code
Thank you in advance
Have a good day
HTML section:
<div class="toolGridContainer">
<div class="containerTitle">Container1</div>
<div class="containerSeparator">
<hr>
</div>
<li id="./images/Image1.svg" class="toolItem ui-draggable ui-draggable-handle">Item 1</li>
<li id="./images/Image2.svg" class="toolItem ui-draggable ui-draggable-handle">Item 2</li>
<li id="./images/Image3.svg" class="toolItem ui-draggable ui-draggable-handle">Item 3</li>
<li id="./images/Image4.svg" class="toolItem ui-draggable ui-draggable-handle">Item 4</li></div>
Javascript:
$(document).on('dragstart', '.toolItem', function(evt){
var imgObj = document.createElement('img');
imgObj.src = $(this).prop('id');
evt.originalEvent.dataTransfer.setDragImage(imgObj, 0, 0);
});
Edit 1: Here is the JSFiddle I made to reproduce my issue
Edit 2: I have made a test div, manually made it draggable and a test function to see if setDragImage is available in other events and this function works fine, why is it?
HTML:
<li id="testItem" draggable="true" class="toolItem">TestItem</li>
Javascript:
var testFunction = function(evt){
console.log('testFunction running');
var dataTransfer = evt.dataTransfer;
var imgObj = document.createElement('img');
imgObj.src = './image.svg';
dataTransfer.setDragImage(imgObj, 0, 0);
}
var testElement = document.getElementById('testItem');
testElement.ondragstart = testFunction;