This is my drag and drop throw fill in blanks code. But the thing is I also want if the user has inserted the wrong word at the wrong place after he/she getting point he/she wants to put a word in the right place. in this code in that type of word removing is not working.
<!DOCTYPE HTML>
<html>
<head>
<style>
.draggable {background-color:#00ff00;margin:5px;padding:3px;}
#div1,#div2 {display:inline-block;min-width:25px;min-height:10px;border-style:solid;border-width:0px;border-bottom-width:2px;}
</style>
<script>
function allowDrop(ev){
ev.preventDefault();
}
function drag(ev){
ev.dataTransfer.setData("Text",ev.target.id);
}
function drop(ev){
ev.preventDefault();
var data=ev.dataTransfer.getData("Text");
ev.target.parentNode.replaceChild(document.getElementById(data), ev.target);
document.getElementById(data).className = "";
}
</script>
</head>
<body>
<span class="draggable" id="drag1" draggable="true" ondragstart="drag(event)">drag</span>
<span class="draggable" id="drag2" draggable="true" ondragstart="drag(event)">drop</span>
<br />
This is a sample
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div> and <div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)"></div> sentence.
</body>
</html>
I added few changes here to make it dynamic, please note that I change the HTML and add CSS I will not go over these simple things, I will describe the javascript changes:
The following will loop through
<i>tag and attacheremoveevent to it:The remove function, will create a new clone of
<i>parent usingcloneNode(), then search for<i>tag and re-attach remove event to it since thecloneNode()method doesn't clone event listeners, then create a new element that will be our duplicate for.draggablethat we replaced suing drop event, then attach all event listeners and attributes to it (class, ondrag, ondrop ... etc), this new div will be placed in back to thetoolboxdiv.One thing to notice here is that once you remove the dropped element it will be place in the wrong order, I will leave this to you fix ;) , Here is what I think you want: