desbest
2008-Jan-16 14:10 UTC
Can someone give me an example of the "replace" feature because the prototype documentation is hard to understand
Can someone give me an example of the "replace" feature because the prototype documentation is hard to understand. ==my failiure i tried== (example taken from api) <html> <head> <script type="text/javascript" src="prototype.js"></script> <script type="text/javascript" > $(''first'').replace(''<ul id="favorite"><li>kiwi</li><li>banana</ li><li>apple</li></ul>''); // -> HTMLElement (p#first) $(''fruits'').innerHTML; // -> ''<ul id="favorite"><li>kiwi</li><li>banana</li><li>apple</li></ ul>'' </script> </head> <body> <div id="food"> <div id="fruits"> <p id="first">Kiwi, banana <em>and</em> apple.</p> </div> </div> </body></html> --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Justin Perkins
2008-Jan-16 15:43 UTC
Re: Can someone give me an example of the "replace" feature because the prototype documentation is hard to understand
On Jan 16, 2008 8:10 AM, desbest <afaninthehouse-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Can someone give me an example of the "replace" feature because the > prototype documentation is hard to understand.This would be typical usage... HTML: <ul> <li id="my-first-item">I have not been replaced, ever!</li> </ul> JavaScript: $(''my-first-item'').replace(''<li id="my-first-item" class="replaced">I have been replaced!</li>''); What it does it literally swap out one element for another. If you give replace a string of HTML (as my example does), then it first creates an element node out of that string, then performs the swap. It really all boils down to this one line of code, which you can easily write on your own: element.parentNode.replaceChild(content, element); If you look at my previous example, then the object $(''my-first-item'') would be the element object in the above snippet and the content object would be the string of HTML I passed in. Hope this helps. -justin --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---