Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in javascript, how do you determine the current event if not passed as arg?

In an arbitrary javascript function, wish to determine the upstream event.

The event listener function did not pass the event do the current function. afaik, window.event is not WC3 and not available.

function listener(e) { subfunc(e.target); }

function subfunc( element) {
    // here I wish to know which if any event responsible for me being called
    var event = ?;
    switch( event.type ) { ... }
}

How does function subfunc() determine the current event?

(Apologize if question asked before - seems it must have been - but cannot track it down.)

like image 929
cc young Avatar asked Oct 09 '22 22:10

cc young


2 Answers

You can do:

function listener(e) { subfunc.call(this, e); }

function subfunc( e ) {
    var element = this; //<- 'this' points to your element, 'e.type' is event type
    switch( e.type ) { ... }
}

Also other way:

function listener(e) { subfunc.call(e, e.target); }

function subfunc( element ) {
    var event = this; //<- 'this' points to your event object
    switch( event.type ) { ... }
}
like image 63
Dmitry Avatar answered Oct 25 '22 05:10

Dmitry


That's not possible. There's only one property which can be accessed through another, reliable way: event.target = this (Provided that the function is called from within the scope of the event listener).

The event object should be passed to subfunc.

like image 36
Rob W Avatar answered Oct 25 '22 07:10

Rob W