Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chain two jquery events

Tags:

jquery

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 () {
  ...
});
like image 541
Ivan Avatar asked Aug 04 '11 14:08

Ivan


People also ask

Can you chain jQuery functions?

With jQuery, you can chain together actions/methods. Chaining allows us to run multiple jQuery methods (on the same element) within a single statement.

Which function is used to chain animations in jQuery?

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.

Which of the following are the characteristics of chaining in jQuery?

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.


2 Answers

$("#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

like image 110
Jacek Kaniuk Avatar answered Oct 18 '22 18:10

Jacek Kaniuk


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/

like image 42
karim79 Avatar answered Oct 18 '22 17:10

karim79