Javascript – How to keep child elements from interfering with HTML5 dragover and drop events

drag and drophtmljavascript

I have a <div> drop target that I have attached 'drop' and 'dragover' events to. The <div> contains an image inside an anchor tag (simplistically: <div><a><img /></a></div>). The child elements of the target seem to block the 'drop' or 'dragover' events from being triggered. Only when the dragged element is over the target, but NOT over its child elements, are both events triggered as expected.

The behavior I would like to achieve is that the 'dragover' and 'drop' events are triggered anywhere over the target <div>, regardless of the existence of child elements.

Best Answer

You can disable the pointer-events in CSS for all children.

.targetDiv * {
    pointer-events: none;
}
Related Topic