Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mouseleave not firing after mouseenter changes HTML within anchor

I'm sure I'm overlooking something but I can't seem to get the "mouseleave" event to fire after I replace the html within the anchor tag that triggered the mouseenter.

Adding code here but really it's much simpler if you visit the JSFiddle link below and hover over the star icons.

$(document).ready(function () {
    $(document).on('mouseenter', '[id^=star-]', function () {

        $('[id^=star-]').html('<span class="star star-empty"></span>');

    }).on('mouseleave', '[id^=star-]', function () {

       $('[id^=star-]').html('<span class="star star-full"></span>');

   });
});

Please see JSFiddle here. Simply hover over the star icons and you shall see what I mean.

like image 387
Adergaard Avatar asked Apr 21 '15 09:04

Adergaard


People also ask

Does mouseenter bubble?

Though similar to mouseover , mouseenter differs in that it doesn't bubble and it isn't sent to any descendants when the pointer is moved from one of its descendants' physical space to its own physical space.

What is the difference between onmouseleave and onmouseout?

The mouseout event triggers when the mouse pointer leaves any child elements as well the selected element. The mouseleave event is only triggered when the mouse pointer leaves the selected element.

What is the working of Mouseleave event in jquery?

The mouseleave event occurs when the mouse pointer leaves the selected element. The mouseleave() method triggers the mouseleave event, or attaches a function to run when a mouseleave event occurs. Note: Unlike the mouseout event, the mouseleave event only triggers when the mouse pointer leaves the selected elements.

What does mouseenter do?

Definition and Usage The mouseenter event occurs when the mouse pointer is over (enters) the selected element. The mouseenter() method triggers the mouseenter event, or attaches a function to run when a mouseenter event occurs..


2 Answers

The mouseleave event works when added

.star {
    display: block;
}

in CSS

Update: Javascript:

$(document).ready(function () {
    $('.rating').on('mouseenter', 'a[id^=star-]', function () {
        console.log('Hello');
        $(this).children('span').addClass('star-empty').removeClass('star-full');
    }).on('mouseleave', 'a[id^=star-]', function () {
        console.log('leave');
        $(this).children('span').addClass('star-full').removeClass('star-empty')
    });
});

Demo: https://jsfiddle.net/zbeyv6fo/

like image 69
Tushar Avatar answered Sep 22 '22 05:09

Tushar


I think that the reason why the mouse leave is not working is because the element which triggered the mouse enter event is removed from the DOM before it can trigger the mouseleave event.

you are replacing the html on mouse enter and the events are still delegated but the element is removed and is not the same element which triggered the mouseenter event so mouseleave is never fired!

like image 21
Kalish Avatar answered Sep 26 '22 05:09

Kalish