If I have an event handler like:
function A() {
...
}
it's possible to assign to more than one event:
$("#test1").keyup(A);
$("#test2").change(A);
But I'm wondering if it's possible to do it with only one sentence, something like:
$("#test1").keyup, $("#test2").change (function () {
...
});
With jQuery, you can chain together actions/methods. Chaining allows us to run multiple jQuery methods (on the same element) within a single statement.
jQuery | chaining() With jQuery, we can use do chaining which means to chain together multiple methods in a single statement on a single element. We have been using a single statement at once, but now using the chaining method we can bind multiple methods to make the code short.
The jQuery provides another robust feature called method chaining that allows us to perform multiple action on the same set of elements, all within a single line of code. This is possible because most of the jQuery methods return a jQuery object that can be further used to call another method.
$("#test2").bind('keyup change', A);
/edit as for different elements and events - it's:
$("#test1, #test2").bind('keyup change', A);
or
$("#test1").bind('keyup', A);
$("#test2").bind('change', A);
depending on what do You expect. There is no simpler way
Yes, yes it is. It is really horrible though.
$("#test1").keyup(A).parent().find("#test2").change(A);
http://jsfiddle.net/8RwZY/
There is also this atrocity:
$("#test1, #test2").eq(0).keyup(A).end().eq(1).change(A);
http://jsfiddle.net/8RwZY/1/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With