This question already has an answer here:
Straight to the business:
I have a jquery event listener that looks like this:
$(".number").click(printNumber);
and a callback function:
function printNumber(number){
console.log(number);
}
I was wondering if I could pass an argument to a callback so it will look something like this
$(".number").click(printNumber(number));
(I know that it immediately invokes that function, but still, is there a way to pass arguments to it)
Thank you in advance!
You can pass data to the callback directly as
event.data
in jQueryAnother way would be to use
bind
But that would also change the callbacks
this
valueThe clean way to handle this is to return a function:
And then use: