This is my code. I have defined two different drop events with different selectors but the parent one overrides the child drop.
//this is parent
for(let i=0 ; i < $(".Droppable").length; ++i)
{
document.querySelectorAll(".Droppable")[i].addEventListener("drop",function (ev) {
ev.preventDefault();
var data = ev.dataTransfer.getData("text");
var copyimg= document.createElement("img");
var orignal= document.getElementById(data);
copyimg.src=orignal.src;
copyimg.alt=orignal.alt;
copyimg.style.zIndex=2
copyimg.classList.add("Droppable2");
ev.target.appendChild(copyimg);
copyimg.style.position="fixed";
copyimg.style.left=ev.clientX+"px";
copyimg.style.top=ev.clientY+"px";
} );
document.querySelectorAll(".Droppable")[i].addEventListener("dragover" ,function(ev){
ev.preventDefault();
});
}
the parent "drop" is for a and child "drop: is for a . I want to assign a particular function when the client drops on the image.
//this is child
for(let i=0;i<$(".Droppable2").length;++i)
{ document.querySelectorAll(".Droppable2")[i].addEventListener("drop",function (ev) {
ev.preventDefault();
var data=ev.dataTransfer.getData('text');
myExperiment.Add(Number(data));
alert("test is" + myExperiment.test+" indication is"+myExperiment.indication);
} );
document.querySelectorAll(".Droppable2")[i].addEventListener("dragover" ,function(ev){
ev.preventDefault();
});
}
This is my fiddle : https://jsfiddle.net/Bhavesh21/1haokeum/5/ the aliceblue div is workspace you can drag appratus and reactants to workspace . if you first drag SampleTestTube to the workspace then drag reagent over it then the listener associated with the image is not working.
on the other hand if you drag Reagent directly over the SampleTestTube in the appratus it works.