I'm working on a simple drag & drop activity that checks to see if all the drag items are in the correct drop zones. Things will get shuffled, but essentially the way I was looking at doing it was each drop zone checks to see if it contains "drag" + the matching array # (ie. the first array item drag0 sits within the first array item [0] drop zone).
However it's only really working for the last item (you can just get the last item right, and you'll get a correct response). I've seen options for wrapping it in a function, or using throw/catch, but nothing seems to work.
item class is draggable items, dropzone is the class for droppable areas.
function seeResult() {
var result = document.getElementById("result");
var drags = document.getElementsByClassName("item");
var drops = document.getElementsByClassName("dropzone");
var i;
for (i = 0; i < drops.length; i++) {
var num = i;
if (drops[i].innerHTML.indexOf("drag" + num)) {
result.style.color = "green";
} else {
result.style.color = "red";
}
}
};
<div class="item" draggable="true" ondragstart="drag(event)" id="drag0">Drag Item 1</div>
<div class="item" draggable="true" ondragstart="drag(event)" id="drag1">Drag Item 2</div>
<div class="item" draggable="true" ondragstart="drag(event)" id="drag2">Drag Item 3</div>
<div class="item" draggable="true" ondragstart="drag(event)" id="drag3">Drag Item 4</div>
<div id="dropZones">
<div id="dropZone1" class="dropcon">
<div class="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div class="dragdesc">
<p>Drop Description 1</p>
</div>
</div>
<div id="dropZone2" class="dropcon">
<div class="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div class="dragdesc">
<p>Drop Description 2</p>
</div>
</div>
<div id="dropZone3" class="dropcon">
<div class="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div class="dragdesc">
<p>Drop Description 3</p>
</div>
</div>
<div id="dropZone4" class="dropcon">
<div class="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div class="dragdesc">
<p>Drop Description 4</p>
</div>
</div>
</div>
<div id="seeResult" onclick="seeResult()">See result!</div>
<div id="result">Result</div>
One last note is that this will be added into a site where content creators can add/remove however many drag/drop items they want, and then spit it out to the end user, so things have to be fairly dynamic, can't just use these 4 specific array items.
Thanks in advance for your help!