Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
55
rated 0 times [  62] [ 7]  / answers: 1 / hits: 17943  / 14 Years ago, sat, october 2, 2010, 12:00:00

I have an <input> which has an onkeydown inline event handler. In this handler, I'd like to call a function and pass a special parameter to it - the event data.



When I want to handle events (e.g. onmousemove) for the whole document, I use the following code:



document.onmousemove=function(e) {
// here I can make a good use of the 'e' variable,
// for example extract the mouse coordinates from it
}


And it works (although I don't know where the e variable - event data - comes from).

But this time I want to use the function only for the <input> mentioned above.

I need to pass the event data to the function so it can get the pressed key's code. And I want to do it in that inline event handler. I've created a function:



function myfunc (e) {
var evt=window.event?event:e;
var code=evt.keyCode;
alert (code);
}


and tried all of these methods:



<input onkeydown=myfunc(this)>



<input onkeydown=myfunc(this.onkeydown)>



<input onkeydown=myfunc(onkeydown)>



But none of them worked, the alert window kept displaying undefined.

I looked for a solution to my problem in Google, but didn't find anything that could help me solve it.


More From » events

 Answers
2

<input onkeydown=myfunc(event)>



function myfunc (e) {
e = e || window.event;
var code = e.keyCode;
alert (code);
}

[#95434] Wednesday, September 29, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
felix

Total Points: 47
Total Questions: 91
Total Answers: 110

Location: Angola
Member since Tue, May 5, 2020
4 Years ago
;