...

View Full Version : Using anonymous function in onkeydown/up to send arguments, how?



ConfusedOfLife
04-09-2003, 02:02 PM
Hi,

this code doesn't have any problem in IE, however, it doesn't work in Mozilla and it doesn't give me any error message.



<body>
<input id="damn" type="Text">
<script>

document.getElementById("damn").onkeydown = function()
{
keydown( event, this );
}
document.getElementById("damn").onkeyup = function()
{
keyup( event, this );
}
function keyup( oEvent, element )
{
var key = oEvent.keyCode;
alert(key);
}
function keydown( oEvent, element )
{
var key = oEvent.keyCode;
alert(key);
}
</script>


As you see, I'm only trying to send some arguments to my functions, but because the syntax doesn't let me do it directly, I have to use an anonymous function. Is there any way I can fix it? I can't use onkeydown/up as the input's attributes directly in the HTML.

liorean
04-09-2003, 03:32 PM
IE uses the global Event object, NN4, Moz and some others pass the event as the first argument of the function instead.

document.getElementById("damn").onkeydown=function(e){
keyevent(e||event,this);
}

document.getElementById("damn").onkeyup=function(e){
keyevent(e||event,this);
}

function keyevent(oEvent,element){
alert(oEvent.type+': '+oEvent.keyCode);
}

Choopernickel
04-09-2003, 04:04 PM
shouldn't both anonymous functions accept the same arguments?

liorean
04-09-2003, 04:11 PM
Yes, they should. I guess I forgot to add it to the other one. It's corrected now.

Choopernickel
04-09-2003, 05:50 PM
Whew! I thought I was coming apart at the seams. I'm still pretty rough with javascript; I hope you didn't take any offense re: my remark. I just read it and realized it sounded a little hurried and harsh.

I've always liked this kind of approach (associating elements with handlers in the script itself, outside the markup), but why not use the attachEvent() method? What's the target browsing environment?

liorean
04-09-2003, 06:04 PM
attachEvent is IE proprietary. W3C uses addEventListener. Most browsers only support window.onevent.

Choopernickel
04-09-2003, 07:17 PM
Oh. Yeah. addEventListener.

*thwack self in head*

ConfusedOfLife
04-09-2003, 09:43 PM
The above sentence is like yelling: YOU ARE COOOOOOOOOL in my language! I'm writing this easyFarsi function, which turns any input/textarea into a Farsi editor. I'm about to finish it and you know when those little problems come up! Just thanks for your help. :thumbsup:

liorean
04-09-2003, 09:55 PM
I'm here to serve - I personally seldom need the help of these forums. They are interesting, though. Beetle, Brothercake, JKD, and a few others as well, are really good sources for nifty tricks, inspiration, and commentary.

In fact, I never had a good look into the ways event handlers worked before you asked a few event handler questions, so I guess you helped me learn something as well.

ConfusedOfLife
04-10-2003, 09:39 AM
:)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum