1

Overview

I want to attach an event to an element for handling file drag and drop events, but it seems like this event is also being attached to all the child elements. For example,

$('#dragtarget').on('dragenter', function() {
    console.log('dragenter');
});

... if I drag files over any elements that are children of the #dragtarget element, the console will output dragenter. So if I attach this event to document or window, the event will fire when hovering over any element on the page.

The same thing happens with dragleave, except when leaving child elements.

Question

Why is this happening? Is there away to attach this event to only the element intended? If not, is there a way to detach the event from all child elements?

jsFiddle

Here's an example fiddle to tinker with: http://jsfiddle.net/UnsungHero97/ePLV6/7/

  • Notice how the border changes to solid when you drag files over the box, but back to dashed when hovering over the text

  • I want the border to change to solid when dragging files over the box, no matter if I'm hovering over the text within the box

Here's the code from the fiddle for just in case:

HTML

<div id="dragtarget">
    <span>File over/out here</span>
    <br/><br/>
    <span>more text here</span>
</div>

JS

// I expect only the #dragtarget element to fire the 'dragenter' and 'dragleave' events,
// not the child elements
$('#dragtarget').on('dragenter', function() {

    $(this).css({borderStyle: 'solid'});

    event.stopPropagation();
    event.preventDefault();
    return false;

}).on('dragleave', function() {

    $(this).css({borderStyle: 'dashed'});

    event.stopPropagation();
    event.preventDefault();
    return false;
});
3
  • 1
    I'm not seeing any border changes in your demo... Commented Jun 3, 2012 at 23:47
  • shoot... I didn't specify that this is in regards to file drag and drop. Commented Jun 4, 2012 at 2:46
  • I also misspelled the selector in the fiddle. Commented Jun 4, 2012 at 2:53

1 Answer 1

2

It doesn't. You're probably seeing event bubbling. Check the event.target to see where the event you're handling originated.

Sign up to request clarification or add additional context in comments.

9 Comments

I don't think that's the issue. I changed the code to stop the event from propagating and nothing changed.
Can you show us the modified code? Because this certainly looks like event bubbling.
@GauravShetty... it's in the jsFiddle, but I've updated my post to include it
@Hristo But you do want it to propogate, you just don't want to flip the border style if the event is fired going out of the div in to the spans. Check out HTML5 dragleave fired when hovering a child element.
@Hristo I don't have a good answer for you there, sometimes things just suck
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.