views:

3777

answers:

5
+10  Q: 

jquery live hover

I'm using the following jquery code to show a contextual delete button only for table rows we are hovering with our mouse. This works but not for rows that have been added with js/ajax on the fly...

Is there a way to make this work with live events?

$("table tr").hover(
        function()
        {

        },
        function()
        {

        }
    );
A: 

Why don't you set same events on ajax-added rows, before appending them to table?

Qwerty
+4  A: 

As of jQuery 1.4.1, the hover event works with live(). It basically just binds to the mouseenter and mouseleave events, which you can do with versions prior to 1.4.1 just as well:

$("table tr")
    .mouseenter(function() {
        // Hover starts
    })
    .mouseleave(function() {
        // Hover ends
    });

This requires two binds but works just as well.

Tatu Ulmanen
+7  A: 

jQuery 1.4.1 now supports "hover" for live() events, but only with one event handler function:

$("table tr").live("hover",
        function()
        {

        }
    );

Alternatively, you can provide two functions, one for mouseenter and one for mouseleave:

$("table tr").live({
        mouseenter:
           function()
           {

           },
        mouseleave:
           function()
           {

           }
       }
    );
Philippe Leybaert
It still does not work for me though. I tried doing this: Where am i going wrong? > $('table tr').live('hover', function() { $(this).find('.deletebutton').toggle(); });
Shripad K
this is incorrect and does not work. see the "Multiple Events" header under the documentation for `live`: http://api.jquery.com/live/
Jason
+2  A: 

This code works:

    $(".ui-button-text").live(
        'hover',
        function (ev) {
            if (ev.type == 'mouseover') {
                $(this).addClass("ui-state-hover");
            }

            if (ev.type == 'mouseout') {
                $(this).removeClass("ui-state-hover");
            }
        });
Jorge Eduardo Cardona
What is "ui-state-hover"? How does that apply to the user's original question?
PythonUser
+19  A: 
$('.hoverme').live('mouseover mouseout', function(event) {
  if (event.type == 'mouseover') {
    // do something on mouseover
  } else {
    // do something on mouseout
  }
});

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

dmitko
this one worked for me, thanks
fearofawhackplanet
Worked for me as well. +1 Tried doing Philippe's version, both with mouseover and mouseenter - neither worked. But this one did. Thanks!
eduncan911
+1 works here as well. Cheers
FFish