3

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
qw3n
  • 6,236
  • 6
  • 33
  • 62

1 Answers1

2

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 .

Community
  • 1
  • 1
qw3n
  • 6,236
  • 6
  • 33
  • 62