By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,968 Members | 1,410 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,968 IT Pros & Developers. It's quick & easy.

setting events

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Wim Roffal" <wi*************@nospamm-planet.nl> writes:
<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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #2

P: n/a
Wim Roffal wrote:
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
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.