Search code examples
javascriptdraggable

Get ID of the previous element when using draggable Javascript


How can I get the ID of the div from where the object has been moved, rather then where it's been dropped? I've used this very simple example to show how I get the current ID that it's in but I want the text to show div1 when the logo is in the right box -because that was the last element it was parented to.

PS. This is my second post here so I'd be more than happy if you could comment on how I could improve my question.

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));
  ev.dataTransfer.setData("text", ev.currentTarget.id);
  var t= ev.currentTarget.id;
  document.getElementById("info").innerHTML=t;
  
}
#div1, #div2 {
  float: left;
  width: 100px;
  height: 35px;
  margin: 10px;
  padding: 10px;
  border: 1px solid black;
}
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)">
  <img src="https://stackoverflow.design/assets/img/logos/so/logo-stackoverflow.svg" draggable="true" ondragstart="drag(event)" id="drag1" width="88" height="31">
</div>

<div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div id="info">This is where I want to know from where the element has been moved.</div>


Solution

  • Where you have...

    ev.target.appendChild(document.getElementById(data));
    

    Replace it with...

    var elementBeingDragged = document.getElementById(data);
    var previousDiv = elementBeingDragged.parentNode; // Get parent DIV before
    ev.target.appendChild(elementBeingDragged); // Appending it to new DIV
    

    The following lines are not necessary but demonstrate that previousDiv is where it came from and its id

    previousDiv.style.backgroundColor = "red";
    console.log(previousDiv.id);
    

    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");
      var elementBeingDragged = document.getElementById(data);
      var previousDiv = elementBeingDragged.parentNode; // Get parent DIV before
      ev.target.appendChild(elementBeingDragged); // Appending it to new DIV
    
      previousDiv.style.backgroundColor = "red";
      console.log(previousDiv.id);
    
      ev.dataTransfer.setData("text", ev.currentTarget.id);
      var t= ev.currentTarget.id;
      document.getElementById("info").innerHTML=t;
      
    }
    #div1, #div2 {
      float: left;
      width: 100px;
      height: 35px;
      margin: 10px;
      padding: 10px;
      border: 1px solid black;
    }
    <div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)">
      <img src="https://stackoverflow.design/assets/img/logos/so/logo-stackoverflow.svg" draggable="true" ondragstart="drag(event)" id="drag1" width="88" height="31">
    </div>
    
    <div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <div id="info">This is where I want to know from where the element has been moved.</div>