up vote 0 down vote favorite

In this abbreviated code, the inline event works - the "event" is passed to the testKeyPress function

<textarea id="source"
    onkeydown= "showCursPos(this); 
    var tf=testKeyPress(event);
    document.onkeypress=function(){return tf};
    document.onkeydown=function(){return tf}; " ></textarea>

function testKeyPress(e){
    if (e.ctrlKey==true ){
        if (e.which == null ){kcode=e.keyCode; }  // IE
        else if (e.which > 0){kcode=e.which; }    // gecko
        return testValidity(kcode);   //returns true-false
    }
}

However, in this anonymous version, the event is not passed in gecko:

<textarea id="source"></textarea>

$("source").onkeydown = function(){ 
    showCursPos(this);  // this function works
    // next event is passed in IE, but not gecko
    var tf=testKeyPress(event); 
    // remaining functions work if value is forced
    document.onkeypress=function(){return tf}; 
    document.onkeydown=function(){return tf}; 
    }

How does one pass the function's own event?

flag

4 Answers

up vote 0 down vote accepted

Yes, there is an event object as arguments.

You can get it by

var e=arguments[0] || event; // Firefox via the argument, but IE don't

I don't know if they exact the same, but I read <xxx onkeydown="func(event);"> as xxx.ononkeydown=function(event){func(event);};

Reference event @ Mozilla.org

link|flag
Your solution worked for me. :-) – Chris Jester-Young Nov 28 '08 at 21:07
up vote 0 down vote

Thanks, I'll take a look and report back later on what happens with that.

link|flag
FYI, answer must be made as comments, otherwise this person won't be notified. – PhiLho Nov 28 '08 at 20:34
up vote 0 down vote

Worked like a charm. This modification to my original code successfully passes the event from the anonymous function to the named function in my four browsers: IE6, FF2, NS7.2, OP9.22

$("source").onkeydown = function(){ 
    var e=arguments[0] || event; 
    showCursPos(this);  
    var tf=testKeyPress(e); 
    document.onkeypress=function(){return tf}; 
    document.onkeydown=function(){return tf}; 
}
link|flag
up vote 0 down vote

Worked well for me!!! Thank you for this post!!!

link|flag

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.