View Single Post
Old 11-14-2010, 12:19 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,574
Thanks: 62
Thanked 4,062 Times in 4,031 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
not true. Or at least half-true.

If you want to pass "this" to the anonymous function, you *must* do so yourself.

Example:
Code:
document.getElementById("bla").onclick = function(e) { myClickHandler(e, this, “John Merlino”); };
Now, having said that, it's really not necessary to pass this as you can always get the object that caused the event from the event object. But it's often more convenient to do so, as if you use the event object you have to write browser-sensitive code.

**********

By the way, the first code example there is wrong:
Code:
if (myDOMNode.onclick)
    myDOMNode.onclick();
It would normally be:
Code:
if (myDOMNode.onclick)
    myDOMNode.click();
That is, you simulate the *event* and that invokes the function that is referenced by the event *handler*.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Users who have thanked Old Pedant for this post:
johnmerlino (11-20-2010)