So, I've been warned that some of my past questions have not been well-received, and I'm in danger of being blocked from asking any more. I hope that this question is well-received and that it doesn't result in me being blocked from asking more questions! I'll do my very best to formulate it as a clear and useful question.
What I'm trying to do is create javascript to click-and-drag images into nested lists. I've read HTML Drag and Drop between multiple Unordered Lists and How to drag and drop into an html unordered list, and neither addresses what I'm trying to do.
The images I want to click-and-drag represent items--some items can contain other items while others cannot. For instance, a Single-Barrel Shotgun can contain Shotgun Ammo, but Shotgun Ammo cannot contain anything.
I've created a jsfiddle, https://jsfiddle.net/pjamesnorris25/9f0y8edz/76/, to facilitate some javascript wizard helping me with this. And because I have, I'm not going to post the javascript here unless someone asks me to do so.
My javascript works if you click-and-drag the shotgun to the "Left Shoulder" in the image of the generic person below the "Shotgun Ammo" and "Single-Barrel Shotgun" images in the upper right-hand corner. That is, when you do so, the text "Single Barrel Shotgun" appears below the "Left Shoulder" in a of its own denoted by a box around the text--the box indicates that "Single Barrel Shotgun" is a container into which you can drag other items, in this case, "Shotgun Ammo".
Prior to dragging the "Single-Barrel Shotgun" graphic to the "Left Shoulder" container, my HTML looks like this:
<span id="leftshoulder-span" style="border:black solid 1px; left:387px; height:30px; width:173px; overflow:hidden; position:absolute; top:96px; " ondrop="drop(event)" ondragover="allowDrop(event)"></span>
<ul id="leftshoulder-ul" style="left:387px; position:absolute; top:126px; z-index:101; "></ul>
After dragging the "Single-Barrel Shotgun" to the "Left Shoulder", the HTML looks like this:
<ul id="leftshoulder-ul" style="left:387px; position:absolute; top:126px; z-index:101; ">
<li id="Single_Barrel_Shotgun_1-li" style="margin-top:2px;">
<span id="Single_Barrel_Shotgun_1-span" ondrop="drop(event)" ondragover="allowDrop(event)" style="border:1px solid black;">
<div class="tooltip">
Single Barrel Shotgun
<span class="tooltipimage">
<img src="https://i.postimg.cc/sGCvntKs/Single-Barrel-Shotgun.gif" id="Single_Barrel_Shotgun_1" class="zoom_small card" draggable="true" ondragstart="drag(event)">
</span>
</div>
</span>
<span id="Single_Barrel_Shotgun_1-contents-span" ondrop="drop(event)" ondragover="allowDrop(event)"></span>
<ul id="Single_Barrel_Shotgun_1-ul"></ul>
</li>
</ul>
Which is exactly what I want. But when I drag the "Shotgun Ammo" image to the "Single Barrel Shotgun" text , I get:
<ul id="leftshoulder-ul" style="left:387px; position:absolute; top:126px; z-index:101; ">
<li id="Single_Barrel_Shotgun_1-li" style="margin-top:2px;">
<span id="Single_Barrel_Shotgun_1-span" ondrop="drop(event)" ondragover="allowDrop(event)" style="border:1px solid black;">
<div class="tooltip">
Single Barrel Shotgun
<span class="tooltipimage">
<img src="https://i.postimg.cc/sGCvntKs/Single-Barrel-Shotgun.gif" id="Single_Barrel_Shotgun_1" class="zoom_small card" draggable="true" ondragstart="drag(event)">
</span>
</div>
</span>
<img id="Shotgun_Shell_4" class="zoom_small card" src="https://i.postimg.cc/N28LZpPT/Shotgun-Shell.gif" draggable="true" ondragstart="drag(event)">
<span id="Single_Barrel_Shotgun_1-contents-span" ondrop="drop(event)" ondragover="allowDrop(event)"></span>
<ul id="Single_Barrel_Shotgun_1-ul">
</ul>
</li>
</ul>
Note that the <img src="https://i.postimg.cc/sGCvntKs/Single-Barrel-Shotgun.gif" ...
is after the </span>
closing the <span class="tooltipimage">
and not in an <li>
inside <ul id="Single_Barrel_Shotgun_1-ul"></ul>
.
That is, what I want is:
<ul id="leftshoulder-ul" style="left:387px; position:absolute; top:126px; z-index:101; ">
<li id="Single_Barrel_Shotgun_1-li" style="margin-top:2px;">
<span id="Single_Barrel_Shotgun_1-span" ondrop="drop(event)" ondragover="allowDrop(event)" style="border:1px solid black;">
<div class="tooltip">
Single Barrel Shotgun
<span class="tooltipimage">
<img src="https://i.postimg.cc/sGCvntKs/Single-Barrel-Shotgun.gif" id="Single_Barrel_Shotgun_1" class="zoom_small card" draggable="true" ondragstart="drag(event)">
</span>
</div>
</span>
<span id="Single_Barrel_Shotgun_1-contents-span" ondrop="drop(event)" ondragover="allowDrop(event)"></span>
<ul id="Single_Barrel_Shotgun_1-ul">
<li id="Shotgun_Shell_4-li" style="margin-top:2px;">
<div class="tooltip">
Shotgun Shell
<span class="tooltipimage">
<img src="https://i.postimg.cc/sGCvntKs/Shotgun-Shell.gif" id="Shotgun_Shell_4" class="zoom_small card">
</span>
</div>
</ul>
</li>
</ul>
What's got me flabbergasted is that the javascript that properly places the "Single-Barrel Shotgun" as an unordered list item under the "Left Shoulder" doesn't place the "Shotgun Shell" as an unordered list item under the "Single-Barrel Shotgun" when the HTML for the two has exactly the same structure. That is, the "Left Shoulder" has a uniquely id
'd <span>
followed by a uniquely id
'd <ul>
:
<span id="leftshoulder-span" style="border:black solid 1px; left:387px; height:30px; width:173px; overflow:hidden; position:absolute; top:96px; " ondrop="drop(event)" ondragover="allowDrop(event)"></span>
<ul id="leftshoulder-ul" style="left:387px; position:absolute; top:126px; z-index:101; "></ul>
and the "Single Barrel Shotgun" also has a uniquely id
'd followed by a uniquely id
'd :
<span id="Single_Barrel_Shotgun_1-contents-span" ondrop="drop(event)" ondragover="allowDrop(event)"></span>
<ul id="Single_Barrel_Shotgun_1-ul"></ul>
I have built in some error checking alerts--that don't work in jsfiddle for some reason; extra bonus point to anyone who can explain this to me. When I run the code on my machine, I get the following when I first click-and-drag the "Single-Barrel Shotgun" to the "Left Shoulder":
which tells me that the javascript is correctly identifying the <span>
to which the "Single-Barrel Shotgun" is being dragged, i.e., the leftshoulder-span
, the code knows the id
of the "Single-Barrel Shotgun" being dropped into the leftshoulder
<ul>
is "Single_Barrel_Shotgun_1" (the "_1" indicates that this is the first of the "Single-Barrel Shotgun"'s that can be dragged, and that the item is a "Single Barrel Shotgun".
But when I click-and-drag the "Shotgun Ammo", the error checking alert I get is:
which, because the eve.target.id
tells me that the code doesn't recognize that it should be putting the "Shotgun Ammo" as the first <li>
in the <ul id="Single_Barrel_Shotgun_1-ul">
.
So my question is: how do I modify my javascript so that when I click-and-drag the "Shotgun Ammo" to the "Single Barrel Shotgun" it appears as an unordered list item under the "Single Barrel Shotgun"? Or put differently, why is eve.target.id
in my error checking alert blank when I click-and-drag the "Shotgun Ammo" to the "Single Barrel Shotgun"?