Lets say I have a javascript function call moveItem which takes two parameters source and destination.
I want to assign this function the the click-event of several buttons, each having different source and destinations.
OK, so without the parameter I would do something like this:
$('#myButton1').click(moveItem);
function moveItem() {
...
}
But what if moveItem looks like this:
function moveItem(source, destination) {
...
}
this doesn't seem to work:
$('#myButton1').click(moveItem('foo','bar'));
You can't do quite what you want. You'll have to use an anonymous function to call moveItem
:
$('#myButton1').click(function(){ moveItem('foo','bar'); };
When registering functions for events, the function signature has to match one of the supported signatures (because code in the click function is calling the handler). Since moveItem
doesn't match, you have to wrap it in an anonymous function that does match.
You want to do
$('#myButton1').click(function() {moveItem('foo','bar')});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With