Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery mouseenter/leave

I have this code in html:

<div class="sub-status">
    <p class="subscribed"><i class="icon-check"></i> Subscribed</p>
</div>

On hover, I want that to be changed to:

<div class="sub-status">
    <p class="unsubscribe"><i>X</i> Unsubscribe</p>
</div>

And, I have this code in jQuery:

$(document).ready(function() {
    $('.sub-status').mouseenter(function() {
        $(this).html("<p class='unsubscribe'><i>X</i> Unsubscribe</p>");
    });
    $('.sub-status').mouseleave(function() {
        $('this').html("<p class='subscribed'><i class='icon-check'></i> Subscribed</p>");
    });
});

The first function is working great. When I mouseover that div, it is changed to what I want, but the mouseleave is not working. I want that when I put my mouse out of that div, its data will return to like it was before. I can't get this working. Any help would be appreciated.

Thanks.

like image 726
Amar Syla Avatar asked Jun 24 '13 13:06

Amar Syla


People also ask

What is Mouseenter and Mouseleave?

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 is Mouseenter event in jQuery?

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..

What is mouse leave?

The mouseleave event is fired at an Element when the cursor of a pointing device (usually a mouse) is moved out of it. mouseleave and mouseout are similar but differ in that mouseleave does not bubble and mouseout does.

What is the difference between mouseover and Mouseenter?

The mouseover event triggers when the mouse pointer enters the div element, and its child elements. The mouseenter event is only triggered when the mouse pointer enters the div element. The onmousemove event triggers every time the mouse pointer is moved over the div element.


1 Answers

Change

$('this')...

to

$(this)...

And you can use hover() instead of using two separate functions:

$('.sub-status').hover(function() {
    $(this).html("<p class='unsubscribe'><i>X</i> Unsubscribe</p>");
},function() {
    $(this).html("<p class='subscribed'><i class='icon-check'></i> Subscribed</p>");
});

Updated

Your fiddle isn't working since you are updating the entire content of the hovered element - update just the text in <p> should work.

$('.sub-status').hover(function() {
  $(this).children('p')
              .removeClass()
              .addClass('unsubscribed')
              .html("<i>X</i> Unsubscribe");
  },function() {
     $(this).children('p')
               .removeClass()
               .addClass('subscribed')
               .html("<i class='icon-check'></i> Subscribed");
});

Working fiddle

like image 116
bipen Avatar answered Sep 21 '22 22:09

bipen