Reputation: 7427
I have a function called monitoring()
as in this fiddle. My question here is, if i would like to call that function, can I simply do this?
$('#status_table #monitor_'+rowID).change(monitoring(rowID));
My logic is that, when the status of the checkboxes(#monitor_'+rowID
is the checkbox ID) are changed, the function will be called.
As i would like to find out whether the function is being called, i do an append as follow:
$('#test').append(fbType + fbNum);
But it does not show up anything for fbType and fbNum in the div #test. Can someone point to me how i should call the function or correct my mistakes in that code/logic?
Upvotes: 0
Views: 108
Reputation: 128
Create a button somewhere to trigger the function then. You can have a better control over the function as well.
Upvotes: 0
Reputation: 30276
Ah the power of closures:
$('#status_table #monitor_'+rowID).change(function() { monitoring(rowID) });
There will be a little more to it than this, as you probably need to actually pass in rowID somewhere; looking at your fiddle, it seemed at first glance as though you'd want to have this snippet inside the function addText
, where you calculate a rowID.
Corrected fiddle (with rowID being utilized): http://jsfiddle.net/EVW4S/
Seems like there are more problems with this code...
One tip that might help in debugging: console.debug
is useful (rather than using alert()
or changing content)
Upvotes: 0
Reputation: 66693
Your current code will call
the function monitoring
and bind its return value as the event handler, which is probably not what you need.
You can instead do it like:
$('#status_table #monitor_'+rowID).change(function() {
monitoring(rowID);
});
Upvotes: 1
Reputation: 160291
change
expects a reference to a function, whereas you're currently calling it and using its return value as the argument to change
–not what you want, since it doesn't return a function.
One option is to create a function generator that takes the row ID and use the return value as the argument to change
, roughly:
function createMonitor(rowId) {
return function() {
// Do something with rowId
};
};
$('#status_table #monitor_'+rowID).change(createMonitor(rowID));
Without knowing where rowID
is coming from it's difficult to know if this is the best approach.
Upvotes: 1