7

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.

1
  • Can you simulate your situation as closely as possible on jsfiddle.net? Commented Nov 1, 2011 at 20:22

8 Answers 8

39

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

Sign up to request clarification or add additional context in comments.

3 Comments

It's important to use "on" event on the element that wasn't dynamicly changed.
In some cases where we want to show/hide stuff, mouseenter/mouseleave combination works better for me
mouseover isn't the proper syntax, mouseenter should be used: api.jquery.com/on/#additional-notes
3

try live

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

Comments

3

Use the live method:

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

A side note hover does take two callback functions, did you mean mouseover

4 Comments

you learn something new everyday..., still wondering what the use case for this would be? Did not have a problem yet, that would have required this.
I use it for when I toggle something like in $(".menu li").hover(function(){$(".submenu",this).toggle()}). Because toggle depends on the current state of the element, it can be used for the hover over and the hover out.
Why does .live('hover' ... works but not .hover on a dynamic element?
As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live(). api.jquery.com/live
1

Use delegate or live to bind the events. This will make sure anything added dynamically will be bound to the event handler as well.

Comments

1

Sounds like you need live or delegate. Delegate is prefered

$(document.body).delegate(".inboxfeedlist li", "hover", function(){
        alert('');
});

Comments

0
$('.inboxfeedlist li').live('hover', function(e) { alert(''); });

jQuery live

jQuery delegate

Comments

0

You could use something like this:

$(document).on('mouseover','div.cover-wrapper',function(){
    $(this).css({'border':'1px solid #000'});
});


$(document).on('mouseout','div.cover-wrapper',function(){
    $(this).css({'border':'none'});
});

Comments

0

Here is the use and details of these functions

http://api.jquery.com/live/

$( selector ).live( events, data, handler ); // jQuery 1.3+

$( document ).delegate( selector, events, data, handler ); // jQuery 1.4.3+

$( document ).on( events, selector, data, handler ); // jQuery 1.7+

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.