Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery, bind same function to 3 different textboxs' keyup event

I have 3 textboxes, and on the keyup event for all 3, I want to call the same function?

In the below code, I am tring to bind keyup event to CalculateTotalOnKeyUpEvent function to a textbox named compensation, but it doesn't work:

$("#compensation").bind("keyup", CalculateTotalOnKeyUpEvent(keyupEvent));

function CalculateTotalOnKeyUpEvent(keyupEvent) {
  var keyCode = keyupEvent.keyCode;
  if (KeyStrokeAllowdToCalculateRefund(keyCode)) {
    CalculateTotalRefund();
  }
};
like image 965
Miral Avatar asked Jul 16 '09 14:07

Miral


2 Answers

You need do like this:

// Edit according to request in the comment: 
// in order to select more than one element,
// you need to specify comma separated ids.
// Also, maybe you need to consider to use a CSS class for the selected elements,
// then it could be just $(".className")
$("#element1, #element2, ....").bind("keyup", CalculateTotalOnKeyUpEvent);

You need to pass the function as a parameter, you do not need to pass the function as it was declared.

like image 102
Artem Barger Avatar answered Sep 19 '22 11:09

Artem Barger


$("#txt1, #txt2, #txt3").keyup(fn);
like image 44
Alex Avatar answered Sep 22 '22 11:09

Alex