jQuery. Different elements, different events, same handler function

Hi,

using jQuery is it possible to execute the same handler function for different element and different events?

it’s clear that I can do with different element with same event and same function:

$('input[type=text], input[type=password], input[type=checkbox], select, textarea').blur(
      function () {
         // do smthg.
      }
   );

but, I would want that some of that element execute that same identical function on blur, and other elements on change.

Sure. You’d probably be better off using a class and setting the class on the elements you want to use it on, but doing it like this would work.

The .blur() function and .change() functions are just shortcuts for the .on() function.

$(document).on('blur change', '.someclass', function() {
    // do something
});

http://api.jquery.com/on/

You can add more if you want. Even custom events.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.