I’m using the native drag and drop API in javascript. How can I remove a dragged element from the DOM following a successful drop?
- I’ve tried listening to the drop event, but this only gets fired on the element which is dropped onto and has no reference to the element being dragged.
- I’ve tried listening to the dragend element, but this doesn’t let me know whether a drop was successful or not.
- I’m trying to avoid storing the element being dragged in a global variable as this will cause problems if a drag occurs between different tabs or browsers.
Here’s an example: http://jsfiddle.net/KNG6n/3/
A list of letters which can be dragged into the box. When a letter’s node is dropped on the box, I’d like it to be removed from the list (without effecting any other list items containing the same letter)
Listen for the dragend event and check the dropEffect variable of the dataTransfer object before doing anything with the dragged element: