javascript - how to get the target id of the ondrop event? -


i'm trying id of target / destination of dragged item. have 2 containers, must ids of 1 has dragged element.

here code:

    <div id="div1" ondrop="drop(event, this)" ondragover="allowdrop(event)"></div>  <div id="div2" ondrop="drop(event, this)" ondragover="allowdrop(event)"></div>     <br/>     <img id="drag1" src="//placehold.it/336x69/ff0000" draggable="true" ondragstart="drag(event)" width="336" height="69" /> 

    function allowdrop(ev) {     ev.preventdefault();     }      function drag(ev) {     ev.datatransfer.setdata('text/html', ev.target.id);     }  function drop(ev, target) {     ev.preventdefault();     console.log(target.id, ev.target.id)      var data = ev.datatransfer.getdata("text/html");     alert(data) } 

the result of code returns drag1 instead of div1 or div2.

what missing?

ev.target.id in drop()is reference destination item.

try code:

 function allowdrop(ev) {     ev.preventdefault(); }  function drop(ev, target) {     ev.preventdefault();     alert(ev.target.id) } 

Comments