How can the "dragenter" event be allowed to pass through child elements?

https://jsfiddle.net/7scfk81L/

The structure of my document is as follows:

<div id="container">
  <div id="inner"></div>
</div>

I have added listeners for dragEnter and dragLeave to the container element.

However, when I drag into the file, it triggers both the container's dragLeaver event and the 'dragEnter' event as it passes through the child element.

Is there a way to prevent this from happening on the parent element?

I attempted adding pointer-events: none to the inner element, but that solution did not meet my desired outcome. I would like the child elements to still be interactable.

Answer №1

After some experimentation, I managed to figure it out on my own

The solution involved setting a switch on a child element event and having the parent element filter events based on the switch

let isChildEntered

container.addEventListener('dragenter', (e) => {
    if (!isChildEntered) {
      console.log('dragenter')
  }
})

container.addEventListener('dragleave', (e) => {
  if (!isChildEntered) {
    console.log('dragleave')
  }
}, true)

inner.addEventListener('dragenter', e => {
    isChildEntered = true
})

inner.addEventListener('dragleave', e => {
    isChildEntered = false
})

Check out the demo here

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Create a custom React component using the CSS grid system

Is there a way to adjust the size of a carousel component and make it smaller using CSS? I tried setting the width in a class but it's not working. How can I achieve this? grid.css .wrapper { display: grid; /* grid-gap: 10px; */ grid-tem ...

Transform a PDF document into a Google Doc utilizing the capabilities of the Google Drive API version 3

I successfully uploaded a PDF file to Google Drive using a node server. However, I am struggling to find a way to convert it to a Google Doc format so that it can be downloaded later as a DOCX document. Here is the code snippet I've been working with ...

Representation of a family tree in CSS showcasing multiple sub-parents (both many to one and one to many relationships)

I've come across various family tree presentations. I'm curious if it's possible to display a one-to-many and then many-to-one structure? Currently, the flow is linear stop after stop, but I'd like to incorporate multiple steps that con ...

Save the uploaded file in Express to a custom directory on the system, avoiding the project's public folder

I have successfully integrated React on the front end to upload a file and receive it in Express.js. However, when storing the file in Express, it is currently being saved in the public folder. If I need to save the files in a non-public folder located at ...

Is there a problem with using Nth-Child / Nth-Of-Type in CSS when classes are dynamically added or removed using jQuery?

Currently, I have a set of LI tags. As users filter the LIs, some may be removed from view. My goal is to assign alternating background colors to the visible LIs. When an LI is visible, it is given the class "LI-Shown." If an LI gets filtered out, I remov ...

What is the maximum amount of information that can be stored in a data attribute within the DOM?

Occasionally, I find myself wanting to include a substantial amount of data on the webpage in order to minimize additional AJAX calls for dynamic content. However, I am concerned about potential performance implications. Is there a penalty I should be aw ...

Utilizing Skeleton to create a cropped text button on an iPhone

I am currently using Skeleton for my simple CSS needs. While it works fine on desktop, I have noticed that on an iPhone, the text in my button gets cropped as shown in the picture. https://i.stack.imgur.com/EYo2P.png Below is the HTML code I'm using ...

React.js router - struggles to clean up unsightly query parameters in the URL

I've been trying to eliminate the query string by following this solution: var { Router, Route, IndexRoute, IndexLink, Link } = ReactRouter; var createHashHistory = History.createHashHistory; var history = createHashHistory({queryKey: false} ...

Utilize a custom attribute in Bootstrap 4 tooltips instead of the standard "title" attribute to define the text displayed in

For my application, I'm utilizing Bootstrap 4 along with tooltips. My objective is to toggle the tooltips individually and only display them upon clicking. The following code allows me to achieve this: <input type="text" id="name" class="form-cont ...

Why isn't my JavaScript variable visible in the HTML code?

I am currently facing an issue with my Angular app where I am trying to display a JavaScript variable in HTML. The variable successfully shows up in an older part of my application, but it fails to do so in the new section. Below is the functional code sn ...

Display items according to the input provided in the form

Consider the following simplified code snippet: <select id="number"> <option value="">Select...</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> ...

Create a heading for the selection menu

I need to customize a dropdown list on my page so that the text "Select Language" is always displayed to the user, regardless of whether they make a selection from the dropdown. Even if the user chooses "Option 1" or "Option 2," the default text should sti ...

Proper method for verifying line-clamp in a Vue component

I came across a question that aligns perfectly with my current task - determining if text is truncated or not. The query can be found here: How can I check whether line-clamp is enabled?. It seems like my approach may not be accurate, so any guidance on ho ...

Guide on setting up JSME, a chemistry portal integrated with JavaScript, within vuejs

I am looking to integrate the JSME chemistry portal into my Vuejs application. Upon the initial page load, an error is displayed: JSME initialization error: HTML id jsme_container not found. Strangely, upon refreshing the page, the error disappears. How ...

What is the best way to implement momentJS globally in VueJS 2?

Currently working with Vue.js version 2.6.11 Trying to set up in main.js as follows: import moment from 'moment' moment.locale('nl'); Object.definePrototype(Vue.prototype, '$moment', { value: moment }); Encountering an error ...

Having trouble establishing a connection between Node.js server and Google Directions API

Recently, I've been facing an issue while trying to establish a connection with the Google Directions API through my node server using a callback function. Surprisingly, it was working fine just a few days back, and now it seems like I accidentally br ...

Retrieving CSV information from several files within a JavaScript directory

Currently, I am attempting to retrieve data from numerous CSV files using 'csvtojson'. Firstly, I gathered an array of file names in a specific directory. Then, I used a forEach loop to extract data from various CSV files and convert it to JSON ...

Tips for sending information to a modal window

I need to transfer the userName from a selected user in a list of userNames that a logged-in user clicks on to a twitter bootstrap modal. I am working with grails and angularjs, where data is populated using angularjs. Set-Up The view page in my grails a ...

Node js does not support iteration for DirectoryFiles

I am currently working on a program aimed at mapping all the json files within a specific directory. As I am new to JavaScript, this inquiry may not be ideal or obvious. Here is my code: const fs = require('fs'); const { glob } = require('gl ...

Create a custom JQuery dropdown menu where the title can be changed and other options are easily

I have a question regarding selecting the tiles while also being able to edit the other and input my own text. Apologies for any confusion... A link to the relevant code is provided here Title Mr Mrs Miss Ms Other ...