qw3n qw3n - 1 year ago 103
Javascript Question

Creating a drag event

How would you simulate a dragevent on a particular element? Here is what I tried, but I keep getting a

e.dataTransfer is null
error when my event handler receives the event.

var event = document.createEvent('DragEvents');
event.initEvent('dragstart', true, false);
document.getElementById('test').dispatchEvent(event);


When I inspect the event from the event handler
dataTransfer
is a property if you use
createEvent('DragEvents')
instead of say
createEvent('Events')
but the property is null.

Answer Source

The document.createEvent('event') is being deprecated. The new method doesn't work because the DragEvent constructor is not meant for actual use.

Check dataTransfer is null when creating drag event programmatically for a similar question .