View Full Version : How to pass the event object when dynamically attaching an event handler in NS6

08-03-2002, 03:10 AM
I have a question regarding passing the event object in NS6 hopefully someone can clarify. I have a function that has a few parameters, one of which is the event object. I wish to dynamically assign this function to an event handler, which itself is attached to a HTML element using script. My question is, how do I pass the event object into this function in this case? The below should work, but it doesn't:

Below code doesn't work in NS6

<form name="test">
<input type="text" name="test2">

function dothis(width,height,e){

document.test.test2.onkeypress=function(e){ dothis(5, 10, event); }


The above returns an "event" not defined error. I have no problem getting things to work if I simply defined the event handler using HTML. For example, the below works:

<input type="text" name="test2" onkeypress="dothis(5,10,event)">

Why is that?

08-03-2002, 04:12 AM

A function with no name? :confused:

08-03-2002, 05:04 AM
anonymous functions are valid as of JavaScript 1.2. i use them all the time for event handlers. but generally not with the event object.

George, the new event handling system still doesn't make sense to me, but from what i get out of the Guide you may have to do that this way:

document.test.test2.onkeypress = function(e) { dothis(5, 10, e); }

might also try it this way:

document.test.test2.onkeypress=function() { dothis(5, 10, event); }

08-03-2002, 05:07 AM
George, read the tutorial I submitted to your very own site. :p

An object named "event" is passed as an argument to whatever function you assign the event to:

Say when mousedown is fire on node "myNode", it does this:


(Essentially, though not really...)

Which means:

myNode.onmousedown = function(anynameyouwantfortheeventobject) {

Or you can forget about naming it, and just call arguments[0].

08-03-2002, 05:46 AM
Thanks guys for the responses.I eventually got to where John is, which works in NS6:

document.test.test2.onkeypress = function(e) { dothis(5, 10, e); }

What still throws me off is why when you attach event handlers using HTML, you need to pass in "event", and not a name holder like e or "ewhateveryouwant", but in the above case, "e' instead in NS6. Specifically, I'm talking about "e" as it occurs here:

dothis(5, 10, e);

I guess my question really boils down to how parameters in anonymous functions operate.

In IE4+, all this is much simplier, as there's simply "event" to deal with.

08-03-2002, 07:22 AM
Every time an event fires, the event object is a unique object. It makes more sense to me to pass this as an argument to the event listener every time, rather than pollute the global namespace with variables which really are better implemented elsewhere.

BTW, this lets you create your own event objects and dispath them programatically without fear of altering the global event object via document.createEvent() and the various initEventType() methods.