Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind multiple events to jQuery 'live' method

Tags:

jquery

jQuery's 'live' method is unable to handle multiple events. Does anyone know of a good workaround to attach multiple events to a function that polls current and future elements? Or am I stuck using duplicate live methods for each event handler I need?

Example - I am trying to do something like:

$('.myclass').live('change keypress blur', function(){
  // do stuff
});
like image 398
Will Peavy Avatar asked Oct 29 '09 19:10

Will Peavy


1 Answers

As of jQuery 1.4.1 .live() can accept multiple, space-separated events, similar to the functionality provided in .bind(). For example, we can "live bind" the mouseover and mouseout events at the same time like so:

$('.hoverme').live('mouseover mouseout', function(event) {
  if (event.type == 'mouseover') {
    // do something on mouseover
  } else {
    // do something on mouseout
  }
});
like image 61
Chris Fulstow Avatar answered Nov 15 '22 13:11

Chris Fulstow