I am trying to drag and drop an image on a div. The image does not get get dragged onto the div and gives the following error
Uncaught TypeError: Failed to e
use ondragstart(event)
instead of ondrag(event)
It's actually a simple answer.
Your function is returning a string rather than the div
node. appendChild
can only append a node.
For example, if you try to appendChild the string:
var z = '<p>test satu dua tiga</p>'; // is a string
document.body.appendChild(z);
The above code will never work. What will work is:
var z = document.createElement('p'); // is a node
z.innerHTML = 'test satu dua tiga';
document.body.appendChild(z);
In my case, there was no string on which i was calling appendChild
, the object i was passing on appendChild
argument was wrong, it was an array and i had pass an element object, so i used divel.appendChild(childel[0])
instead of divel.appendChild(childel)
and it worked. Hope it help someone.
This can happen if you accidentally are not dragging the element that does have an id assigned (for example you are dragging the surrounding element). In that case the ID is empty and the function drag() is assigning an empty value which is then passed to drop() and fails there.
Try assigning the ids to all of your elements, including the tds, divs, or whatever is around your draggable element.
You may also use element.insertAdjacentHTML('beforeend', data);
Please read the "Security considerations" on MDN.