/*
* Because "dragend" is called both if you drop the item on the dropzone
* or if you drop the item anywhere else, we have to mind if we can
* remove it.
*
* When the item is successfuly dropped on the dropzone, dnd_successful
* is true and we can remove it safely.
*/
dnd_successful = false;
/*
* Set all the <span> and <img> DnD events up.
* Most of the browser have already set <img> DnD events up, but we
* override them to define a specific behavior.
*/
var draggables = [];
var spans = document.getElementsByTagName('span');
var imgs = document.images;
for(var k = 0; k < spans.length; k++)
draggables.push(spans[k]);
for(var j = 0; j < imgs.length; j++)
draggables.push(imgs[j]);
for(var i = 0; i < draggables.length; i++)
{
draggables[i].addEventListener('dragstart', function(event){
event.target.style.background = "green";
/* Allow specific type of transfers */
event.dataTransfer.effectAllowed = "move";
/*
* Set the data to send to the dropzone.
* Two cases :
* 1 - it is a picture, make a text/html data string.
* 2 - it is plain text, make a simple text/plain data string.
*/
var src = event.target.getAttribute('src');
var alt = event.target.getAttribute('alt');
if(null !== src) //Case 1
event.dataTransfer.setData("text/html", '<img src="' + src + '" alt="' + alt + '"/>');
else //Case 2
event.dataTransfer.setData("text/plain", event.target.innerText || event.target.textContent);
dnd_successful = false;
});
draggables[i].addEventListener('dragend', function(event){
if(dnd_successful)
event.target.parentNode.removeChild(event.target);
else
event.target.style.background = 'white';
});
}
var dropzone = document.getElementById('mydropzone');
dropzone.addEventListener('dragenter', function(event){
event.target.style.background = "#CCCCCC";
event.preventDefault();
});
dropzone.addEventListener('dragleave', function(event){
event.target.style.background =...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.