Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery.Hover not working for dynamic Element

Here is my code

$(".inboxfeedlist li").hover(function(e){alert('');}

This is not working for dynamically created elements, even i have use

$(".inboxfeedlist li").bind('hover',function(){})

is also not working, what's problem with code.

like image 595
Wasim A. Avatar asked Nov 01 '11 20:11

Wasim A.


2 Answers

live become deprecated at jQuery 1.9. We can use on with mouseenter and mouseleave events instead:

$(document).on("mouseenter", ".save-btn", function(e) {
    $(this).css("background-image","url('ie/imgs/btn/hover-btn.png')");
    $(this).find("a").css("background-image","url('ie/imgs/btn/hover-btn-left.png')");
});

$(document).on("mouseleave", ".save-btn", function(e) {
    $(this).css("background-image","url('ie/imgs/btn/btn.png')");
    $(this).find("a").css("background-image","url('ie/imgs/btn/btn-left.png')");
});

For some reason I can't use hover with on. It simply doesn't work. But, from what I have read, hover is just an adaptation from mouseenter and mouseleave, so it is fine. (https://stackoverflow.com/a/4463384/1031340)

If you do not need to support IE6, I recommend you use :hover on your CSS (if it is a change only in CSS, how example above).

like image 105
eduardomozart Avatar answered Sep 19 '22 18:09

eduardomozart


try live

$(".inboxfeedlist li").live('hover',function(){});
like image 3
swatkins Avatar answered Sep 21 '22 18:09

swatkins