views:

2301

answers:

5

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

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

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

function CalculateTotalOnKeyUpEvent(keyupEvent) {
        var keyCode = keyupEvent.keyCode;
        if (KeyStrokeAllowdToCalculateRefund(keyCode)) {
            CalculateTotalRefund();
        }
    };

Thanks..

+4  A: 

You need do like this:

// edit according to request in the comment 
// in order to select more than one element you need to specify id's comma separated
// as well may be you need to consider to use class for selected elements
// then it could be just $(".className")

    $("#element1, #element2, element3, ....")  
          .bind("keyup",CalculateTotalOnKeyUpEvent);

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

Artem Barger
+1 beat me to it.
geowa4
probably should illuminate on how to get all three textboxes though.
geowa4
You, right, I just didn't want to do it, since there was an answers after me which pointed that out.
Artem Barger
i tried this $("#compensation","#adminFee", "#reimbursement").bind("keyup",CalculateTotalOnKeyUpEvent);and its not working
Miral
You doesn't do it correct. Look at mine example. It's only one string and id's separated by commas within and not several string separated by commas.
Artem Barger
Thanks it worked my mistake
Miral
+2  A: 
$("#txt1, #txt2, #txt3").keyup(fn);
Alex
A: 

You are calling CalculateTotalOnKeyUpEvent immediately, not passing it as an argument. Try:

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

joshski
you got beat. delete your answer out of respect.
geowa4
How do I do that?
joshski
A: 
$("#compensation").bind("keyup",CalculateTotalOnKeyUpEvent(keyupEvent));

When you write CalculateTotalOnKeyUpEvent(keyUpEvent) [notice the () after function name], you are executing the CalculateTotalOnKeyUpEvent and assigning the result of the function to the key up event.

You should do something like,

$('#compensation, #otherId1, #otherId2')
    .bind('keyup', CalculateTotalOnKeyUpEvent);

Where 'otherId1' and 'otherId2' are the ids of two more textboxes.

SolutionYogi
A: 

or just give all 3 text boxes the same class and you good to go

Namconox