setting events

W

Wim Roffal

I have an object

<button id=summer>

Now I want to dynamically set an event for this object:

function foo()
{
}
myobject = document.getElementById("summer");
myobject.onClick = foo;

Problem is that I want to give the function an argument. "foo(this)" instead
of just "foo".

How can I achieve that?

Thanks,

Wim
 
L

Lasse Reichstein Nielsen

Wim Roffal said:
<button id=summer>
Now I want to dynamically set an event for this object: ....
myobject = document.getElementById("summer");
myobject.onClick = foo;

Problem is that I want to give the function an argument. "foo(this)" instead
of just "foo".

---
var self = this; // save reference in variable
myobject.onclick = function(){ foo(self); };
---
Notice:
"onclick" must be all lower case.
The function *is* given one argument: the event. Here it is discarded.
(except in IE, where the event is a global variable instead)

/L
 
M

Mike Foster

Wim said:
I have an object

<button id=summer>

Now I want to dynamically set an event for this object:

function foo()
{
}
myobject = document.getElementById("summer");
myobject.onClick = foo;

Problem is that I want to give the function an argument. "foo(this)" instead
of just "foo".

How can I achieve that?


The Event object is the only argument passed to a listener.
However, there are ways to accomplish what I think you want.
Have a look at this demo:
http://cross-browser.loc/toys/tristateimage.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top