155

Trying to figure out how to use the Jquery .on() method with a specific selector that has multiple events associated with it. I was previously using the .live() method, but not quite sure how to accomplish the same feat with .on(). Please see my code below:

$("table.planning_grid td").live({
  mouseenter:function(){
     $(this).parent("tr").find("a.delete").show();
  },
  mouseleave:function(){
     $(this).parent("tr").find("a.delete").hide();        
  },
  click:function(){
    //do something else.
  }
});
       

I know I can assign the multiple events by calling:

 $("table.planning_grid td").on({
    mouseenter:function(){  //see above
    },
    mouseleave:function(){ //see above
    }
    click:function(){ //etc
    }
  });

But I believe the proper use of .on() would be like so:

   $("table.planning_grid").on('mouseenter','td',function(){});

Is there a way to accomplish this? Or what is the best practice here? I tried the code below, but no dice.

$("table.planning_grid").on('td',{
   mouseenter: function(){ /* event1 */ }, 
   mouseleave: function(){ /* event2 */ },
   click: function(){  /* event3 */ }
 });
starball
  • 20,030
  • 7
  • 43
  • 238
butangphp
  • 1,573
  • 2
  • 10
  • 8

6 Answers6

273

That's the other way around. You should write:

$("table.planning_grid").on({
    mouseenter: function() {
        // Handle mouseenter...
    },
    mouseleave: function() {
        // Handle mouseleave...
    },
    click: function() {
        // Handle click...
    }
}, "td");
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • 1
    Why isn't the selector `$("table.planning_grid td")` ? – Muers Dec 22 '11 at 18:20
  • 12
    @Muers, it would also work and the questioner acknowledges so, but also believes that he should bind the event on the `` instead of each individual `
    ` element, which is indeed the right way to go.
    – Frédéric Hamidi Dec 22 '11 at 18:23
  • 39
    There is a good reason to use the .on on the and not on the
    and it's if you dynamically add s later. $('table td').on... will only effect the that are in the table the moment you call this function. $('table').on(... ,'td',function...) will effect any you will add to this table later.
    – Raanan May 11 '12 at 10:31
  • 8
    @Raanan, indeed, and in addition every event handler one registers has a cost, albeit tiny. When you're dealing with thousands of cells, registering a single handler on the `` instead of one handler per `
    ` element becomes mandatory to achieve usable performance.
    – Frédéric Hamidi Aug 17 '12 at 19:10
  • @FrédéricHamidi This was a hard problem to Google; but I finally found your answer. Not even the jQuery docs was clear on this. Thank you! – AVProgrammer Sep 27 '12 at 22:37
  • 2
    Agreed. Very difficult to google this question as 'on' is such a common word and most results were related to `.bind` and `.live`. Good answer, just what I was looking for :D @Frédéric - Your link doesn't link to a header `id` on the jquery docs page anymore. Probably a result of updated documentation. But I could not find this answer on that page. – nzifnab Jan 24 '13 at 18:01
  • Good job using the `selector` parameter. I am now using this approach for most Form event handlers and using the selector parameter to lazy attach. – GoldBishop Feb 07 '17 at 18:17
213

Also, if you had multiple event handlers attached to the same selector executing the same function, you could use

$('table.planning_grid').on('mouseenter mouseleave', function() {
    //JS Code
});
Turnerj
  • 4,258
  • 5
  • 35
  • 52
Pirijan
  • 3,430
  • 3
  • 19
  • 29
32

If you want to use the same function on different events the following code block can be used

$('input').on('keyup blur focus', function () {
   //function block
})
Nayantara Jeyaraj
  • 2,624
  • 7
  • 34
  • 63
thakurdev
  • 420
  • 4
  • 7
13

I learned something really useful and fundamental from here.

chaining functions is very usefull in this case which works on most jQuery Functions including on function output too.

It works because output of most jQuery functions are the input objects sets so you can use them right away and make it shorter and smarter

function showPhotos() {
    $(this).find("span").slideToggle();
}

$(".photos")
    .on("mouseenter", "li", showPhotos)
    .on("mouseleave", "li", showPhotos);
matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
Iman
  • 17,932
  • 6
  • 80
  • 90
9

And you can combine same events/functions in this way:

$("table.planning_grid").on({
    mouseenter: function() {
        // Handle mouseenter...
    },
    mouseleave: function() {
        // Handle mouseleave...
    },
    'click blur paste' : function() {
        // Handle click...
    }
}, "input");
angelmedia
  • 957
  • 8
  • 10
1

Try with the following code:

$("textarea[id^='options_'],input[id^='options_']").on('keyup onmouseout keydown keypress blur change', 
  function() {

  }
);
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Yogesh
  • 289
  • 4
  • 13