1

I have a js file that has .on("click" , ..) chaining happening that I would like to also add a hover event to. My code is:

}).on('hover' , '.tooltip' , function (e) {
   if (e.type == "mouseenter") {
      var tip = $(this).attr('title');
      var tipTemp = $(this).attr('data-title', tip);
      $('#tooltip').remove();
      $(this).parent().append('<div id="tooltip">' + tipTemp + '</div>');
      $(this).removeAttr('title');      
      $('#tooltip').fadeIn(300);
    }else{
      $(this).attr('title', $(this).attr('data-title'));
      $('#tooltip').fadeOut(300);
      $(this).removeAttr('data-title');
    }
});

I understand that I can really only pass one function this way so I am checking for the event type to trigger the appropriate behavior. This doesn't seem to be working for me. Any ideas?

Rob M
  • 121
  • 1
  • 2
  • 13

4 Answers4

1

I think this is what you want

}).on('mouseenter' , '.tooltip' , function (e) {
    var tip = $(this).attr('title');
    var tipTemp = $(this).attr('data-title', tip);
    $('#tooltip').remove();
    $(this).parent().append('<div id="tooltip">' + tipTemp + '</div>');
    $(this).removeAttr('title');      
    $('#tooltip').fadeIn(300);
});

}).on('mouseleave' , '.tooltip' , function (e) {
    $(this).attr('title', $(this).attr('data-title'));
    $('#tooltip').fadeOut(300);
    $(this).removeAttr('data-title');
});
OutFall
  • 482
  • 7
  • 20
  • This did it. Dipak's solution probably would work too but I wanted to uphold the chaining in the code. N0ir's suggestion did the trick. – Rob M Jul 15 '15 at 00:21
0

You can bind several event handlers at once. In your case it will be:

.on('mouseenter mouseleave' , '.tooltip' , function (e) { ... });
dfsq
  • 191,768
  • 25
  • 236
  • 258
0

You don't need if (e.type == "mouseenter") {

And hover is not a valid method to use with .on() - I am not sure about this though.. use mouseover or mouseenter

Use it as:

$('.tooltip-holder').on('mouseover' , '.tooltip' , function () {
      var tip = $(this).attr('title');
      var tipTemp = $(this).attr('data-title', tip);
      $('#tooltip').remove();
      $(this).parent().append('<div id="tooltip">' + tipTemp + '</div>');
      $(this).removeAttr('title');      
      $('#tooltip').fadeIn(300);      
});

$('.tooltip-holder').on('mouseout' , '.tooltip' , function () {
      $(this).attr('title', $(this).attr('data-title'));
      $('#tooltip').fadeOut(300);
      $(this).removeAttr('data-title');
});

Fiddle

Dipak
  • 11,930
  • 1
  • 30
  • 31
0

As per jQuery source code, hover is not included in the event list that triggered leading to JQuery .on() because .hover() is just a shortcut for JQuery .mouseenter() and .mouseleave(). So, in short, you cannot use hover with .on() like below:

}).on('hover' , '.tooltip' , function (e) {...

So, your option is to use .mouseenter() and .mouseleave() like below:

.on({
    mouseenter: function () {
        //stuff to do on mouse enter
    },
    mouseleave: function () {
        //stuff to do on mouse leave
    }
}, '.tooltip');
lshettyl
  • 8,166
  • 4
  • 25
  • 31