I'm using the example provided on the w3schools site to generate a two-column Drag / Drop (DIV ). The objects that will be moved between these DIVs are identified by the IDs from 1 to 5.
So far everything is working as it should, the problem occurs when the user moves one item over the other.
EX: The user drags the ID item 1 and drops over the ID item 2, instead of being passed the ID of the DIV and takes the ID of the P element.
log result:
Correto: ITEM: 1 - COLUNA: DIV2 Errado: ITEM: 2 - COLUNA: 1
I would like to know if it is possible to block the P element so that it is not targeted by the drop, or to get the ID of the DIV even if the item is 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>