Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 563
RMD Developer Avatar asked Feb 09 '11 07:02

RMD Developer


People also ask

Does html5 support drag and drop?

Complete HTML/CSS Course 2022 Now HTML 5 came up with a Drag and Drop (DnD) API that brings native DnD support to the browser making it much easier to code up. HTML 5 DnD is supported by all the major browsers like Chrome, Firefox 3.5 and Safari 4 etc.

How is preventDefault() used with drag and drop?

Most areas of a web page or application are not valid places to drop data. Thus, the default handling of these events is not to allow a drop. Calling the preventDefault() method during both a dragenter and dragover event will indicate that a drop is allowed at that location.


1 Answers

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

.targetDiv * {     pointer-events: none; } 
like image 125
Bastiaan Avatar answered Nov 24 '22 16:11

Bastiaan