This is a simple listener:
$("#target").keypress(function(event) {
alert("test");
});
I want to take separate function out but it won’t work:
$("#target").keypress(doAlert(event));
function doAlert(event)
{
alert("test");
}
What am I missing?
You want:
Your original:
…calls the
doAlertfunction and passes its return value intokeypress, just like any other function call. You want to givekeypressa reference to thedoAlertfunction, you don’t want to give itdoAlert‘s return value. (IfdoAlertcreated and returned an event handler function you might do that, but that’s not what you’ve written.)