Hi all I am trying to get the following code working but somehow it doesn''t.. Please help me. ------------ snip --------- Ajax.Request(''/json/images/''+query, { onComplete:function(xmlhttp,json) { if(json.TotalResults != 0) { json.Item.each(function(item) { $(''out'').appendChild( Builder.node(''div'', {id:item.ImgId,className:"imageResult"}, [ Builder.node(''img'', {src:item.MediumImage[0].URL} ), Builder.node(''p'', item.ItemAttributes.Title ) ]) ); Event.observe(item.ImgId, ''click'', function(e){ console.log(''clicked ''+ this.id) }) }); } else { $(''out'').innerHTML = ''<p class="notice">Nothing found</p>''; } } }); ------------ /snip --------- As you can see i am trying to create a list of images and add an onclick event to the DIV i just built within the each loop. But it is not working like that. I have the feeling the code doesn''t even get executed.. To my understanding Event.observe shouldnt fail because I append the element before adding the event so it should be able to find it. If i run Event.observe after onComplete is done (takes a second) then it works, so the code for Event.observe is fine, just on in context.. I would like to have the onComplete to do it all at once. Could someone please help me getting this right? TIA -- Regards, Kjell --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs -~----------~----~----~----~------~----~------~--~---
m3nt0r.de-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Sep-30 22:23 UTC
Re: Builder.node, each() and Event.observe - Help!
Okay.. i got it. document.getElementById insted of the the "imgId" works. No idea why it is not perfoming this by iteself within the onComplete Handler. Event.observe(document.getElementById(item.ImgId), ''click'', function(e){ .. }); Thanks for reading tho. :) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs -~----------~----~----~----~------~----~------~--~---
Christophe Porteneuve
2006-Sep-30 23:08 UTC
Re: Builder.node, each() and Event.observe - Help!
m3nt0r.de-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org a écrit :> Okay.. i got it. > > document.getElementById insted of the the "imgId" works. No idea why it > is not perfoming this by iteself within the onComplete Handler.a) Welcome to the wonderful world of JS scoping. The trouble with your former code was, item is a *reference*, that got used by *all* observe calls in your loop, therefore *all* observations were on the latest item you looped over (didn''t try, but it''s a common issue, so this is the likely cause). b) Cut that down by using the $ function instead of document.getElementById: Event.observe($(item.ImgId), ...); More Prototypish :-) ''HTH -- Christophe Porteneuve a.k.a. TDD "[They] did not know it was impossible, so they did it." --Mark Twain Email: tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs -~----------~----~----~----~------~----~------~--~---
Christophe Porteneuve wrote:> m3nt0r.de-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org a écrit : > > Okay.. i got it. > > > > document.getElementById insted of the the "imgId" works. No idea why it > > is not perfoming this by iteself within the onComplete Handler. > > a) Welcome to the wonderful world of JS scoping. The trouble with your > former code was, item is a *reference*, that got used by *all* observe > calls in your loop, therefore *all* observations were on the latest item > you looped over (didn''t try, but it''s a common issue, so this is the > likely cause).Thanks for the insight on the reference issue. I thought that it would use the value of item.property in the first place. I will be more careful next time :-) Regards, Kjell --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs -~----------~----~----~----~------~----~------~--~---