Prevent child element from being dragged when parent is draggable
Believe it or not, but you actually need to set draggable="true"
on the input and then add an event handler for dragstart
on the input where you run event.preventDefault()
:
<div
onDragStart={
(e) => { this.bound_handleDragStart.call(this, e) }}
draggable="true"
>
<div>Stuff here</div>
<div><input draggable="true" onDragStart={
(e) => e.preventDefault()
}/></div>
</div>
This will prevent the actual dragging, but the dragstart
event on the parent will still be triggered since it bubbles up. If you want to prevent that too you need to also call event.stopPropagation()
.
How to avoid a child div in react invoking a parent's `onDragLeave` event
The simplest way to do this would be to set pointerEvents: "none"
in the style
prop of the child <div>
, so that all pointer-related events on this div are ignored. That would introduce some limitations, though - like you couldn't bind click events to the child div.
If this doesn't fit within the constraints of your application, you can modify your onDragLeave
handler to inspect the relatedTarget
of the event (see docs). If the drag is leaving the parent <div>
but entering a child of the parent, then the relatedTarget
(i.e. child div), will be contained (see docs) by the currentTarget
(i.e. the parent <div>
)
Here's the code that should work:
const onDragLeave = (event) => {
preventDefaults(event);
// This condition will be true when the drag leaves the parent for a child,
// but false when the drag leaves the parent for somewhere else.
if (event.currentTarget.contains(event.relatedTarget)) return;
console.log("LEAVE");
setDragOver(false);
};
See this codesandbox.
Drag and Drop 'dragover' event to catch the draggable parent and not the child when I drag over an item? Simple Example Inside
We can go up the hierarchy until we are satisfied with the results:
document.addEventListener('dragover', function (event) { allowDrop(event); }, true);
function allowDrop(ev)
{ el = ev.target;
while ((el.tagName !== "LI") && (el.tagName !== "BODY")) el = el.parentNode;
document.getElementById("result").innerHTML = el.innerHTML;
}
Fiddle: https://jsfiddle.net/Lnrt2zx1/
Related Topics
Get Count of True Values in Json With JavaScript
How to Make a Modal Popup to Take an Input Value Using Jquery or JavaScript
Why Is It a Bad Practice to Return Generated HTML Instead of Json or Is It
Export Method/Function in React Native
How to Count Duplicate Value in an Array in JavaScript
How to Conditionally Include Script Element in Angular
Resource Blocked Due to Mime Type Mismatch (X-Content-Type-Options: Nosniff)
Get Selected Value from Multiple Select on Change in Dynamic Form
How to Parse Excel (Xls) File in Javascript/Html5
Using Ajax to Post a Variable to Another PHP File
How to Create a Horizontal Scrolling Chart.Js Line Chart With a Locked Y Axis
Click on Element Containing Text in Puppeteer
Can We Insert JavaScript into Any Webpage Loaded in the Browser
How to Set the Multi Sliders in One Page
How to Get Input Value Dynamically Using Jquery