Hello everyone. Can you help me with a little problem that''s almost driving me mad? Please take a look at http://taewoo.org.ua There you can see empty UL and dynamically added to it LI. In that LI there is link, that adds to UL another LI, and so on. When I click at the link of first element, it creates the second. After that link of first element strops working. So allways works link only of last created element. And all LI''s that was created before loose their event handler. Can anyone tell me what am I doing wrong? Thanks in advance! --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
Dan Dorman
2007-Nov-30 19:05 UTC
Re: Event handling of dynamically created elements question
On Nov 30, 2007 11:30 AM, r8 <storchay-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Can anyone tell me what am I doing wrong?I think the problem''s on this line: this.container.innerHTML += ''<li>[SNIP]</li>''; When you append text to the innerHTML like that, I _think_ the old innerHTML is removed from the document and replaced with the revised innerHTML. The side effect of this is that all the original elements are pulled out of the DOM and replaced with clones--but the clones don''t have the event handlers of attached, since they''re technically different elements (who just happen to have the same IDs as the elements they''ve replaced). Easy fix, fortunately! Just do either: this.container.insert(''<li>[SNIP]</li>''); Or: this.container.insert(new Element(''li'', { className: ''Node'', id: ''Node_'' + id }) .insert(new Element(''a'', { className: ''link_add'', href: ''#'' }) .insert(''+''))); Which is perhaps a little more standards friendly, seeing as how it''s inserting elements rather than fiddling with innerHTML. (Incidentally, the latter syntax also requires Prototype 1.6). Hope that helps! :Dan Dorman --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
> Easy fix, fortunately!Wow, thanks! It''s all works! I can''t believe it was such a foolish mistake. Many thanks! --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---