Skip to content Skip to sidebar Skip to footer

Js Mouseenter Triggered Twice

The problem is about event mouseenter which is triggered twice. The code is here : http://jsfiddle.net/xyrhacom/ HTML :
text1

Solution 1:

use stopPropagation(); Prevents the event from bubbling up the DOM tree,

$(document).ready(function() {
    $(".elt").mouseenter(function(e) {
       e.stopPropagation();
        console.log($(this).attr('val'));
    });
})

Updated demo

Solution 2:

Both #elt1 and #elt2 have your selector class (.elt ) use event.stopPropagation() to stop event from bubbling up in the DOM tree

$(document).ready(function() {
    $(".elt").mouseenter(function(event) {
        event.stopPropagation();
        console.log($(this).attr('val'));
    });
})

Solution 3:

If you only want to let the first child trigger the event, you can use a selector like:

$(".elt > .elt")

Solution 4:

The issue here is that elt2 is inside elt1, and the mouseenter event is bubbling up the DOM chain. You need to stop the bubbling by using event.stopPropagation() to prevent your function from firing multiple times:

$(document).ready(function() {
    $(".elt").mouseenter(function(e) {
        e.stopPropagation();

        console.log($(this).attr('val'));
    });
})

I've made a fiddle here: http://jsfiddle.net/autoboxer/9e243sgL/

Cheers, autoboxer

Post a Comment for "Js Mouseenter Triggered Twice"