How can you pass parameters into the keydown method from jquery because whenever I use a variable defined elsewhere it returns undefined. I assume its because the #target is window and therefore its not in the scope but even then I have trouble getting it to compare the key.which() with an outside parameter and then assigning it to another property.
Example:
var a = 38;
var b = 0;
$(document).keydown(function(e){
var key = e.which;
if (a==key)
b=key;
});
console.log(a+""+b);
Whenever I try to do something along the same lines it returns 38 0 which I interpreted as it not being in the scope and being undefined (also because if I log b it prints undefined in the keydown func)? How could I pass in a?
Try this
Edit
Try this
You
console.log
is not working because it is initialized when the script load. What you need to do is to trigger your functions when the key is pressed.Do did you mean to have the console.log inside the
keydown
function?I was able to get it to respond (as I would expect) like this:
within the scope of the keydown function, a and b get set properly if you hit the up-arrow.
I suspect that what was happening was that your console.log happened on page load. Then after that, you initialized the binding and so you never saw the updated results.