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.
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 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” .
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.
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).
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();
}
});
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With