First of all... set up your event handler in a variable so you can detach it
when it's no longer needed (good practice to get into). Secondly, use the
prototype function "bindAsEventListener" to get the event passed into the
handler (and to keep "this" in scope), like so...
var myHandler = function(event)
{
event.srcElement.style.border = '1px solid white';
}.bindAsEventListener(this);
Event.observe(imgs[i], "click", myHandler);
...then in your dispose (or cleanup stage when these elements or the click
handler is no longer needed), you can do...
Event.stopObserving(imgs[i], "click", myHandler);
myHandler = null;
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Dirk Eschler
Sent: Thursday, February 02, 2006 6:33 AM
To: [email protected]
Subject: [Rails-spinoffs] this pointer in Event.observe function
Hello,
please consider the following code example. It applies the onclick handler to
all image tags, and through window.event it ensures that it works in IE too:
--------------------------------------------------
var imgs = $('foo').getElementsByTagName('img');
for(var i=0; i<imgs.length; i++) {
// Apply onclick handler
imgs[i].onclick=function() {
var id = this.id || window.event;
$(id).style.border = '1px solid white';
}
}
--------------------------------------------------
This works just fine. Now how can i achieve the same with Event.Observe? I
have replaced the "Apply onclick" part with the code pieces listed below.
Unfortunately they don't work in IE, it throws 'null or not an object' in
both cases.
1)-----------------------------------------------
// Apply onclick handler
Event.observe(imgs[i], 'click', function(){
this.style.border = '1px solid white';
}, false);
-------------------------------------------------
2)-----------------------------------------------
// Apply onclick handler
Event.observe(imgs[i], 'click', function(){
var id = this.id || window.event;
$(id).style.border = '1px solid white';
}, false);
-------------------------------------------------
I have the feeling i'm missing some important background information here. Can
the event be passed to the anonymous function, or how does it work? Any help
is appreciated.
--
Dirk Eschler zeitform Internet Dienste
mailto:[EMAIL PROTECTED] Fraunhoferstraße 5
PGP S/MIME: http://key.zeitform.de/ap 64283 Darmstadt, Germany
Tel./Fax: +49 (0) http://www.zeitform.de
_______________________________________________
Rails-spinoffs mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
The information transmitted in this electronic mail is intended only for the
person or entity to which it is addressed and may contain confidential,
proprietary, and/or privileged material. Any review, retransmission,
dissemination or other use of, or taking of any action in reliance upon,
this information by persons or entities other than the intended recipient
is prohibited. If you received this in error, please contact the sender and
delete the material from all computers.
_______________________________________________
Rails-spinoffs mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs