2
I am using the example provided on the website of w3schools, to generate a Drag/Drop with two columns (DIV). The objects that will be moved between these DIV’s are identified by Ids 1 to 5.
So far this is all working as it should, the problem occurs at the moment the user moves one item over the other.
EX: The user drags the ID item 1 and drops it over the ID item 2, instead of passing the DIV ID and grabbing the ID of the P element.
log result:
Correto: ITEM: 1 - COLUNA: DIV2 Errado: ITEM: 2 - COLUNA: 1
I wonder if it is possible to block the P element so that it is not dropped, or to pick up the ID of the DIV even the item being dropped on another item.
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.appendChild(document.getElementById(data));
console.log("ITEM: " + data + " - COLUNA: " + ev.target.id);
}
#DIV1, #DIV2 {
float: left;
width: 150px;
height: 400px;
margin: 10px;
padding: 10px;
border: 1px solid black;
}
p {
color:red
}
<div id="DIV1" ondrop="drop(event)" ondragover="allowDrop(event)">
<h3><center>INICIO</center></h3>
<hr>
<p draggable="true" ondragstart="drag(event)" id="1">UM</p>
<p draggable="true" ondragstart="drag(event)" id="2">DOIS</p>
<p draggable="true" ondragstart="drag(event)" id="3">TRES</p>
<p draggable="true" ondragstart="drag(event)" id="4">QUATRO</p>
<p draggable="true" ondragstart="drag(event)" id="5">CINCO</p>
</div>
<div id="DIV2" ondrop="drop(event)" ondragover="allowDrop(event)">
<h3><center>MEIO</center></h3>
<hr>
</div>