Hello everyone, I know a little Javascript and I have a problem with a function of drag and drop. Let me explain it. I have a page where I have a sytem of drag and drop with images. Each image are in div and I have to put them in an other div. After putting all my images on a div, I have a button (like a form) and when I click on this button, I want to collect information. For example, I want to know in each div my first image is. I want the same for the second image and the other ones. Here is my javascript code :
<script> function allowDrop(ev){ ev.preventDefault(); } function drag(ev){ ev.dataTransfer.effectAllowed='move'; ev.dataTransfer.setData("Text", ev.target.id); } function drop(ev){ ev.preventDefault(); var data=ev.dataTransfer.getData("Text"); previousParent = document.getElementById(data).parentNode; if (ev.target.nodeName=="IMG") { // here nb parentNode.childNodes !=0 ! for (i=0; i<ev.target.parentNode.childNodes.length; i++) { var node = ev.target.parentNode.childNodes[i]; if (node.nodeName=="IMG") { ev.target.parentNode.insertBefore(document.getElementById(data), node); ev.target.parentNode.removeChild(node); if (!RegExp("fake").test(node.id)) { previousParent.appendChild(node); } break; } } } else { // check if div is not empty for (i=0; i<ev.target.childNodes.length; i++) { var node = ev.target.childNodes[i]; if (node.nodeName=="IMG") { ev.target.insertBefore(document.getElementById(data), node); ev.target.removeChild(node); alert (node.id+":"+RegExp("fake").test(node.id)); if (!RegExp("fake").test(node.id)) { previousParent.appendChild(node); } break; } } // div is empty, add drag ev.target.appendChild(document.getElementById(data)); } } </script>
Collect information after drag and drop
in JavaScript
Posted · Edited by stess29
Hello everyone, I know a little Javascript and I have a problem with a function of drag and drop. Let me explain it. I have a page where I have a sytem of drag and drop with images. Each image are in div and I have to put them in an other div. After putting all my images on a div, I have a button (like a form) and when I click on this button, I want to collect information. For example, I want to know in each div my first image is. I want the same for the second image and the other ones. Here is my javascript code :
The divs where my image are at first :
And the divs where my images are after the drag and drop :
Is there a solution to my problem ? Thank you in advance for your answers.