From an input text I need to call a function to capture the onkeydown event and I need to pass a parameter to that event, for example:
<input type="text" onkeydown="TextOnKeyDown('myPrefix');" />...
Then in that function I need to know what key was pressed. I use
function TextOnKeyDown(prefix) {
var key = event.keyCode;
...
}
This works like a charm in IE, but not in Firefox. I've read that for firefox you have to pass an argument to the handler and then use it to get the key, something similar to:
<input type="text" onkeydown="detectKey(event)"/>
...
function detectKey(e){
var key = (document.all) ? e.keyCode : e.which;
...
}
But I cannot get to pass my parameter and the needed event parameter for firefox. Any suggestions?
One more twist here that combines getting the event and the keyCode:
You can use
arguments[0]
:To get the IE event in there too you could do:
Or change your
detectKey(e)
function to check ife
is set and useevent
if it's not.I use this in firefox and it works:
and then the js funcion:
None of the proposed solutions worked as what I needed was to add my own parameter apart from the necessary "event" parameter for Firefox to get the keyCode. Anyway the helped, thanks guys. I put the final solution for future readers:
In the end I just had to add the "event" parameter to the caller:
and the handler:
Somehow it didn't work before, I don't know why.
I wrote this yesterday, works in both FF and IE =>
you will want to replace document.onkeydown with [your input].onkeydown