Becky
Becky

Reputation: 5595

Pass a value via .bind

How can I pass a value via .bind to a keyup function??

Here is an example of what I'm trying to achieve.

var a = 10;

$(document).on("keyup", ".tfs", function(a) {
   if(a != 8){ //trying to show that it was not via paste
     a = 10; // if not pasted keep value 10
   }
})

$('.tfs').on('paste', function(a) {
   a = 8; // if pasted pass value 8
});

Upvotes: 0

Views: 50

Answers (1)

Sylvia Stuurman
Sylvia Stuurman

Reputation: 137

How to pass data to an eventhandler is documented in the documentation of the jQuery API: https://api.jquery.com/on/ scroll to Passing data to the handler

However, you can only read those data. In your case, you could make use of the fact that you declared a as a global variable: you don't need to pass a to the function, because a is available from within the function.

In your case, if you would make use of the jQuery API:

$(document).on("keyup", ".tfs", {myVar: a}, function() {
 if(myVar != 8) { 
   myVar = 10; // does not have an effect on the value of a...
 }
})

$('.tfs').on('paste', {myVar: a}, function() {
   myVar = 8; // does not have an effect on the value of a...
});

Upvotes: 1

Related Questions