dataTransfer is null when creating drag event programmatically

Viewed 4751


I am trying to create a dragEvent and fire it programmatically using this code on Chrome:

var ev = document.createEvent("MouseEvents");
ev.initEvent("dragstart");

and then fire it this way:

element.dispatchEvent(ev);

The element has draggable attribute.

The event was dispatched successfully. However on the event being fired - the dataTransfer object is null. Even when I try to set it manually to a working dataTransfer object - it still stays null.

Any ideas?
Thanks

2 Answers

Looks like it can work like this:

const dataTransfer = new DataTransfer;
dataTransfer.setData("data", '1');
cell.dispatchEvent(new DragEvent('drop', { dataTransfer: dataTransfer }));
Related