Tobie Langel
2006-Jul-07 20:22 UTC
Event.observe(element, ''click'', fn); vs. element.onclick = fn;
Hi all, I''m generating a new element through javascript upon initializing a class. This element needs to react to an onclick event. Is there a difference in using this: element = document.createElement("li"); element.onclick = this.handleClick.bindAsEventListener(this); $(''container'').appendChild(element); rather then this: element = document.createElement("li"); $(''container'').appendChild(element); Event.observe(element, ''click'', this.handleClick.bindAsEventListener(this)); If so, what is it, and which solution should I favor. (From what I understand skimming through the code, Event.observe() deals with IE leakage by using an onunload event, while the first solution would not, is this correct?) Thanks and best regards, Tobie
Ryan Gahl
2006-Jul-07 20:26 UTC
Re: Event.observe(element, ''click'', fn); vs. element.onclick = fn;
Correct, and it also allows you to attach multiple event handlers to a single element/event pair, as well as detach them at will. It''s just a more robust model. On 7/7/06, Tobie Langel <tobie.langel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi all, > > I''m generating a new element through javascript upon initializing a > class. This element needs to react to an onclick event. > > Is there a difference in using this: > > element = document.createElement("li"); > element.onclick = this.handleClick.bindAsEventListener(this); > $(''container'').appendChild(element); > > rather then this: > > element = document.createElement("li"); > $(''container'').appendChild(element); > Event.observe(element, ''click'', > this.handleClick.bindAsEventListener(this)); > > If so, what is it, and which solution should I favor. > > (From what I understand skimming through the code, Event.observe() > deals with IE leakage by using an onunload event, while the first > solution would not, is this correct?) > > Thanks and best regards, > > Tobie > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs >_______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Tobie Langel
2006-Jul-09 00:53 UTC
Re: Event.observe(element, ''click'', fn); vs. element.onclick = fn;
Thanks Ryan for clearing up this issue. regards, Tobie> Date: Fri, 7 Jul 2006 15:26:25 -0500 > From: "Ryan Gahl" <ryan.gahl-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > Subject: Re: [Rails-spinoffs] Event.observe(element, ''click'', fn); vs. > element.onclick = fn; > To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Message-ID: > <b09d0490607071326y61cbc0bfib51776a0c8488006-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org> > Content-Type: text/plain; charset="iso-8859-1" > > Correct, and it also allows you to attach multiple event handlers to a > single element/event pair, as well as detach them at will. It''s just a > more > robust model. > > On 7/7/06, Tobie Langel <tobie.langel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> Hi all, >> >> I''m generating a new element through javascript upon initializing a >> class. This element needs to react to an onclick event. >> >> Is there a difference in using this: >> >> element = document.createElement("li"); >> element.onclick = this.handleClick.bindAsEventListener(this); >> $(''container'').appendChild(element); >> >> rather then this: >> >> element = document.createElement("li"); >> $(''container'').appendChild(element); >> Event.observe(element, ''click'', >> this.handleClick.bindAsEventListener(this)); >> >> If so, what is it, and which solution should I favor. >> >> (From what I understand skimming through the code, Event.observe() >> deals with IE leakage by using an onunload event, while the first >> solution would not, is this correct?) >> >> Thanks and best regards, >> >> Tobie >> >> _______________________________________________ >> Rails-spinoffs mailing list >> Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs >>