39

I'm changing my codes to be compatible with jQuery 1.8 and I'm stuck with this hover which doesn't work. When I used then same thing with a click it worked. Here is my code, can anyone tell me where I'm going wrong?

$(document).on('hover', '.top-level', function (event) {
  $(this).find('.actionfcnt').show();
  $(this).find('.dropfcnt').show();
}, function () {
  $(this).find('.dropfcnt').hide('blind', function () {
    $('.actionfcnt').hide();
  });
});
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Param Veer
  • 776
  • 4
  • 13
  • 27

5 Answers5

75

Deprecated as of jQuery 1.8: The name "hover" used as a shorthand for the string "mouseenter mouseleave". It attaches a single event handler for those two events, and the handler must examine event.type to determine whether the event is mouseenter or mouseleave. Do not confuse the "hover" pseudo-event-name with the .hover() method, which accepts one or two functions.

Source: http://api.jquery.com/on/#additional-notes

That pretty much says it all, you cant use "hover" for that:

$(document).on('mouseenter','.top-level', function (event) {
    $( this ).find('.actionfcnt').show();
    $( this ).find('.dropfcnt').show();
}).on('mouseleave','.top-level',  function(){
    $( this ).find('.dropfcnt').hide('blind', function(){
        $('.actionfcnt').hide();
    });
});
nbrooks
  • 18,126
  • 5
  • 54
  • 66
  • suppose there is a ajax call and the content of .top-level is replaced with new then also this mouseenter would work? – Param Veer Sep 04 '12 at 09:27
  • @param Yes this will work fine, this syntax still fully supports delegated events (events triggered by elements which weren't in DOM at time of function def). This will accomplish what you want for dynamically generated elements. – nbrooks Sep 04 '12 at 09:29
  • thanks you for the answer :) just checked it using dynamic content , its working perfect – Param Veer Sep 04 '12 at 09:53
  • @ParamVeer Glad it was useful! :) – nbrooks Sep 04 '12 at 09:55
  • This thing was burning my head for days, i was 100% that my code was working and it did when I replaced "hover" with mouseenter. I still don't get why they removed when they could just improve the name or at least throw a damn error. Anyway Thanks a lot man! – John M. Apr 04 '21 at 13:23
11

there is no "hover" event. there is .hover() function that takes 2 callbacks (as in your example).

Dziad Borowy
  • 12,368
  • 4
  • 41
  • 53
5

.on function has only 3 parameters : http://api.jquery.com/on/

If you don't need your handlers be bound to dynamically added elements as well, then you may use the good old hover function with 2 event handlers.

$('.top-level').hover(function (event) { 
  $(this).find('.actionfcnt').show();
  $(this).find('.dropfcnt').show();
}, function (event) {   
  $(this).find('.dropfcnt').hide('blind', function(){
    $('.actionfcnt').hide();
  });
});​

By the way, $(selector).hover(handlerIn, handlerOut) is shorthand for $(selector).mouseenter(handlerIn).mouseleave(handlerOut);.

If you need to, then use on for mouseenter and mouseleave events:

$(document).on('mouseenter', '.top-level', function (event) { 
  $(this).find('.actionfcnt').show();
  $(this).find('.dropfcnt').show();
}).on('mouseleave', '.top-level', function (event) {   
  $(this).find('.dropfcnt').hide('blind', function(){
    $('.actionfcnt').hide();
  });
});​
nbrooks
  • 18,126
  • 5
  • 54
  • 66
Danil Speransky
  • 29,891
  • 5
  • 68
  • 79
5

Try:

$(".top-level").on({
    mouseenter: function (event) {
        $( this ).find('.actionfcnt').show();
        $( this ).find('.dropfcnt').show();
    },
    mouseleave: function (event) {
        $( this ).find('.dropfcnt').hide('blind', function(){
            $('.actionfcnt').hide();
        });
    }
});

OR

$(".top_level").on("hover", function(event) {
  if(event.type == "mouseenter") {
    $( this ).find('.actionfcnt').show();
    $( this ).find('.dropfcnt').show();
  }
  else if (event.type == "mouseleave") {
    $( this ).find('.dropfcnt').hide('blind', function(){
        $('.actionfcnt').hide();
    });
  }
});
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
1

Try

$('.top-level').hover(function (event) {
        $( this ).find('.actionfcnt').show();
        $( this ).find('.dropfcnt').show();
}, function(){
        $( this ).find('.dropfcnt').hide('blind', function(){
            $('.actionfcnt').hide();
        });
});
Florian Bauer
  • 626
  • 3
  • 12