Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8037843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:02:46+00:00 2026-06-05T03:02:46+00:00

Overview I have the following HTML structure and I’ve attached the dragenter and dragleave

  • 0

Overview

I have the following HTML structure and I’ve attached the dragenter and dragleave events to the <div id="dropzone"> element.

<div id="dropzone">
    <div id="dropzone-content">
        <div id="drag-n-drop">
            <div class="text">this is some text</div>
            <div class="text">this is a container with text and images</div>
        </div>
    </div>
</div>

Problem

When I drag a file over the <div id="dropzone">, the dragenter event is fired as expected. However, when I move my mouse over a child element, such as <div id="drag-n-drop">, the dragenter event is fired for the <div id="drag-n-drop"> element and then the dragleave event is fired for the <div id="dropzone"> element.

If I hover over the <div id="dropzone"> element again, the dragenter event is again fired, which is cool, but then the dragleave event is fired for the child element just left, so the removeClass instruction is executed, which is not cool.

This behavior is problematic for 2 reasons:

  1. I’m only attaching dragenter & dragleave to the <div id="dropzone"> so I don’t understand why the children elements have these events attached as well.

  2. I’m still dragging over the <div id="dropzone"> element while hovering over its children so I don’t want dragleave to fire!

jsFiddle

Here’s a jsFiddle to tinker with: http://jsfiddle.net/yYF3S/2/

Question

So… how can I make it such that when I’m dragging a file over the <div id="dropzone"> element, dragleave doesn’t fire even if I’m dragging over any children elements… it should only fire when I leave the <div id="dropzone"> element… hovering/dragging around anywhere within the boundaries of the element should not trigger the dragleave event.

I need this to be cross-browser compatible, at least in the browsers that support HTML5 drag-n-drop, so this answer is not adequate.

It seems like Google and Dropbox have figured this out, but their source code is minified/complex so I haven’t been able to figure this out from their implementation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T03:02:48+00:00Added an answer on June 5, 2026 at 3:02 am

    I finally found a solution I’m happy with. I actually found several ways to do what I want but none were as successful as the current solution… in one solution, I experienced frequent flickering as a result of adding/removing a border to the #dropzone element… in another, the border was never removed if you hover away from the browser.

    Anyway, my best hacky solution is this:

    var dragging = 0;
    
    attachEvent(window, 'dragenter', function(event) {
    
        dragging++;
        $(dropzone).addClass('drag-n-drop-hover');
    
        event.stopPropagation();
        event.preventDefault();
        return false;
    });
    
    attachEvent(window, 'dragover', function(event) {
    
        $(dropzone).addClass('drag-n-drop-hover');
    
        event.stopPropagation();
        event.preventDefault();
        return false;
    });
    
    attachEvent(window, 'dragleave', function(event) {
    
        dragging--;
        if (dragging === 0) {
            $(dropzone).removeClass('drag-n-drop-hover');
        }
    
        event.stopPropagation();
        event.preventDefault();
        return false;
    });
    

    This works pretty well but issues came up in Firefox because Firefox was double-invoking dragenter so my counter was off. But nevertheless, its not a very elegant solution.

    Then I stumbled upon this question: How to detect the dragleave event in Firefox when dragging outside the window

    So I took the answer and applied it to my situation:

    $.fn.dndhover = function(options) {
    
        return this.each(function() {
    
            var self = $(this);
            var collection = $();
    
            self.on('dragenter', function(event) {
                if (collection.size() === 0) {
                    self.trigger('dndHoverStart');
                }
                collection = collection.add(event.target);
            });
    
            self.on('dragleave', function(event) {
                /*
                 * Firefox 3.6 fires the dragleave event on the previous element
                 * before firing dragenter on the next one so we introduce a delay
                 */
                setTimeout(function() {
                    collection = collection.not(event.target);
                    if (collection.size() === 0) {
                        self.trigger('dndHoverEnd');
                    }
                }, 1);
            });
        });
    };
    
    $('#dropzone').dndhover().on({
        'dndHoverStart': function(event) {
    
            $('#dropzone').addClass('drag-n-drop-hover');
    
            event.stopPropagation();
            event.preventDefault();
            return false;
        },
        'dndHoverEnd': function(event) {
    
            $('#dropzone').removeClass('drag-n-drop-hover');
    
            event.stopPropagation();
            event.preventDefault();
            return false;
        }
    });
    

    This is clean and elegant and seems to be working in every browser I’ve tested so far (haven’t tested IE yet).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following HTML structure: (jade template language) #review-item-1.review-item a.submit-review-toggle(href=#).review-item-toggle + h3 Service
I am following the steps on http://developer.android.com/sdk/ndk/overview.html to build the hello-jni sample, however when
I have the following: <a href=/Product/Overview>Overview</a> <a href=/Product/Review>Review</a> When I am on the overview
At my personal website while viewing a single post, i have an overview DIV,
Overview I have an iOS project with a table view with the following specification:
I have the following list and would like to remove an element from it.
I have recently done an overview of my site security following the recent news
Overview I have an iOS project which contains 2 navigation controllers as shown in
I have a overview page containing a list with some links from which multiple
I have a default overview.xaml loaded into a frame with a listview and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.