TL:DR: I need help making sure a parent element is the target of a drop event, not its children.
Background: I am trying to create a project stimulated by discussion on this forum about object-oriented Javascript. It is to be a single-page app in React. I have been bashing my head against drag-and-drop functionality, and making quite a dent. Unfortunately, I’ve hit some harder stone, and I can’t figure out why drag-and-drop functionality doesn’t bubble up to the level in the DOM tree where I’ve implemented my onDragOver and onDrop handlers. I experimented a little to see whether this was a React issue or a Javascript issue. It appears to be a Javascript Issue, as you can see in this pen here:
Note: I strongly recommend docking the editor panes to the left (bottom left icon under the “change view” button).
Expected behavior:
I’d expect the drop events to “fall through” to the highest level of the DOM tree that implements the drop handlers (parent level).
Actual behavior:
As you’ll see in the pen, the child (blue) div can receive drop events even though the handlers for those drop events are implemented on the parent (red) div. The grandparent(green) div behaves as expected, receiving no drop events.
Whenever you drop the “Drag Me” (gray) span onto the parent or child divs, a new <p> element is appended to it indicating that a drop event occurred, and what the id of the drop target was. Once you’ve done this, those programmatically-generated <p> nodes are displayed, and can receive the drop, as they are descendants of the parent level.
If you look at the console, you’ll see each of these drag-n-drop events should bubble
and are cancelable
. I haven’t found a good answer despite googling, or I wouldn’t have gone through the bother of running this vanilla JS experiment as a supplement to my question.