Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-03-2002, 02:10 AM   PM User | #1
WA
Administrator


 
Join Date: Mar 2002
Posts: 2,596
Thanks: 2
Thanked 19 Times in 18 Posts
WA will become famous soon enough
How to pass the event object when dynamically attaching an event handler in NS6

Hi;
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
Code:
<form name="test">
<input type="text" name="test2">
</form>

<script>
function dothis(width,height,e){
alert(e)
}

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

</script>
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:
Code:
<input type="text" name="test2" onkeypress="dothis(5,10,event)">
Why is that?
__________________
- George
- JavaScript Kit- JavaScript tutorials and 400+ scripts!
- JavaScript Reference- JavaScript reference you can relate to.
WA is offline   Reply With Quote
Old 08-03-2002, 03:12 AM   PM User | #2
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,220
Thanks: 4
Thanked 80 Times in 79 Posts
Spookster will become famous soon enough
function(e){

A function with no name?
__________________
Spookster
CodingForums Supreme Overlord
All Hail Spookster
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is offline   Reply With Quote
Old 08-03-2002, 04:04 AM   PM User | #3
joh6nn
wei wu wei


 
joh6nn's Avatar
 
Join Date: Jun 2002
Location: 72° W. 48' 57" , 41° N. 32' 04"
Posts: 1,887
Thanks: 0
Thanked 1 Time in 1 Post
joh6nn is an unknown quantity at this point
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); }
__________________
bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

i am a loser geek, crazy with an evil streak,
yes i do believe there is a violent thing inside of me.
joh6nn is offline   Reply With Quote
Old 08-03-2002, 04:07 AM   PM User | #4
jkd
Senior Coder

 
jkd's Avatar
 
Join Date: May 2002
Location: metro DC
Posts: 3,163
Thanks: 1
Thanked 18 Times in 18 Posts
jkd will become famous soon enough
George, read the tutorial I submitted to your very own site.

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:

myNode.mousedown(event);

(Essentially, though not really...)

Which means:

myNode.onmousedown = function(anynameyouwantfortheeventobject) {
alert(thesamenameasbefore)
}

Or you can forget about naming it, and just call arguments[0].
__________________
jasonkarldavis.com
jkd is offline   Reply With Quote
Old 08-03-2002, 04:46 AM   PM User | #5
WA
Administrator


 
Join Date: Mar 2002
Posts: 2,596
Thanks: 2
Thanked 19 Times in 18 Posts
WA will become famous soon enough
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.
__________________
- George
- JavaScript Kit- JavaScript tutorials and 400+ scripts!
- JavaScript Reference- JavaScript reference you can relate to.

Last edited by WA; 08-03-2002 at 04:51 AM..
WA is offline   Reply With Quote
Old 08-03-2002, 06:22 AM   PM User | #6
jkd
Senior Coder

 
jkd's Avatar
 
Join Date: May 2002
Location: metro DC
Posts: 3,163
Thanks: 1
Thanked 18 Times in 18 Posts
jkd will become famous soon enough
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.
__________________
jasonkarldavis.com
jkd is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:34 PM.


Advertisement
Log in to turn off these ads.