$(document).ready(function()
{
var test_func=function(ev)
{
alert(ev.keyCode);
};
….
$(document).keydown(test_func(ev));
});
I wanna do the next, if I press somebutton on keyboard, I’ll see alert with a code of key which I pressed. But I see only ‘ev is not defined’ in my firebug =|
What do you think about this?
It should look like this (no parameters in the call):
The event will be passed as the first argument, and you can use
ev.whichsince jquery normalizes this across browsers 🙂When you call a function like this you want to pass the function itself as what to call when the event happens, so use
method. If you usemethod(something)it’s trying to invoke the method right then (with a variableev, that it can’t find) and assign the result of that method as the event handler, rather than the method itself.You could also use an anonymous method, like this: