Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery live hover

I can't seem to convert the following into a live hover

$("li.favorite_item").hover(
    function () {
        $(this).append($(" <a href='#' class='button'>x</a>"));
    }, 
    function () {
        $(this).find("a:last").remove();
    }
);

I've tried:

$("li.favorite_item"").live('hover', function() { 
    function () {
        $(this).append($(" <a href='#' class='button'>x</a>"));
    }, 
    function () {
        $(this).find("a:last").remove();
    }
});

But it does not work.

like image 560
oshirowanen Avatar asked Dec 15 '10 13:12

oshirowanen


People also ask

Does jQuery handle hover?

jQuery hover() MethodThe hover() method specifies two functions to run when the mouse pointer hovers over the selected elements. This method triggers both the mouseenter and mouseleave events. Note: If only one function is specified, it will be run for both the mouseenter and mouseleave events.

Is jQuery hover deprecated?

Is hover deprecated in jQuery? Deprecated in jQuery 1.8, removed in 1.9: The name “hover” used as a shorthand for the string “mouseenter mouseleave” .

What is the jQuery equivalent of Onmouseover?

jQuery mouseover() Method The mouseover() method triggers the mouseover event, or attaches a function to run when a mouseover event occurs. Note: Unlike the mouseenter event, the mouseover event triggers if a mouse pointer enters any child elements as well as the selected element.

What is the difference between Mouseout and Mouseleave?

This means that mouseleave is fired when the pointer has exited the element and all of its descendants, whereas mouseout is fired when the pointer leaves the element or leaves one of the element's descendants (even if the pointer is still within the element).


2 Answers

From jQuery 1.7+ .live() is deprecated, and .delegate() has been superseded by the .on() method.

Use .on() and .off() in place of .live(), and .die(). Use .on() in place of .delegate().

Converting older code is straightforward as explained here.


You need to call the events that .hover() maps to separately, like this:

$("li.favorite_item").live('mouseenter', function() { 
  $(this).append($(" <a href='#' class='button'>x</a>"));
}).live('mouseleave', function () {
  $(this).find("a:last").remove();
});

.hover() isn't an event function like .click() is for example, it's just a special shortcut for .mouseenter(handler1).mouseleave(handler2)...so you need to do the same in your .live() call.

If you're on jQuery 1.4.3+, you can use a map to simplify things, like this:

$("li.favorite_item").live({
  mouseenter: function() { 
    $(this).append($(" <a href='#' class='button'>x</a>"));
  },
  mouseleave: function () {
    $(this).find("a:last").remove();
  }
});

Also, if this is on a specific <ul>, .delegate() is a better option, like this:

$("#myUL").delegate("li.favorite_item", {
  mouseenter: function() { 
    $(this).append($(" <a href='#' class='button'>x</a>"));
  },
  mouseleave: function () {
    $(this).find("a:last").remove();
  }
});
like image 106
Nick Craver Avatar answered Oct 27 '22 01:10

Nick Craver


The .live() syntax was nicer, but we have to use .on(), now.

You can use an event map on the document, with the selector as the 2nd argument:

$(document).on({
    mouseenter: function () {
        $(this).append("<a href='#' class='button'>x</a>");
    },
    mouseleave: function () {
        $(this).find("a:last").remove();
    }
}, "li.favourite_item");
like image 45
twf Avatar answered Oct 27 '22 00:10

twf