Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining two event handlers, jQuery

I have two events that I want to observe on an object,

input.blur(function(event) {
  ajaxSubmit(this);
  event.preventDefault();
});

form.submit(function(event) {
  ajaxSubmit(this);
  event.preventDefault();
});

But I feel like this isn't dry enough, can I bind two events to my object input and execute my function ajaxSubmit() if either fire?


What would be super cool is if you could do:

input.bind("blur", "submit", function(event) {
  ajaxSubmit(this);
  event.preventDefault();
});
like image 487
JP Silvashy Avatar asked Dec 08 '22 05:12

JP Silvashy


1 Answers

You can use the on method

$("#yourinput").on("blur submit", functionname);

Can combine any number of events; separate them with a space.

like image 164
rahul Avatar answered Dec 28 '22 07:12

rahul