I''m using the Insertion object and need to get the element just inserted. The object appears to just return the parent object used. How can I get the new element? Thanks! Daniel --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
There are many ways but which is best in your case depends on how much knowledge you have of what is being inserted and where you are inserting it. Probably the best bet is to use Prototype''s DOM traversal methods. Element.next/previous/up/down are good because they don''t select text nodes and if you know what you are looking for you can use CSS rules with them to find it easily. http://prototypejs.org/api/element/methods/next With Insertion.Bottom, if you are inserting more than one new element you may need to store a reference to the old lastChild and then call Element.next on it. var oldLast = element.lastChild; new Insertion.Bottom(element,content); //or new Insertion.After(oldLast,content); var firstNew = Element.next(oldLast); All of the other cases should be very straightforward. E.g. if Top use down, if After use next, if Before use previous Colin Daniel Eben Elmore wrote:> I''m using the Insertion object and need to get the element just inserted. > The object appears to just return the parent object used. How can I get the > new element? > > > Thanks! > Daniel > > > > > > . > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I''m primarily using Insertion.Bottom and don''t see how to use the traversal methods because I don''t know when I''ve reached the last element. I really don''t know anything about the element being inserted. It''s an HTML snippet returned from the server. Any ideas? -----Original Message----- From: rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Colin Mollenhour Sent: Sunday, February 25, 2007 3:25 AM To: rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Subject: [Rails-spinoffs] Re: Get Newly Inserted Element There are many ways but which is best in your case depends on how much knowledge you have of what is being inserted and where you are inserting it. Probably the best bet is to use Prototype''s DOM traversal methods. Element.next/previous/up/down are good because they don''t select text nodes and if you know what you are looking for you can use CSS rules with them to find it easily. http://prototypejs.org/api/element/methods/next With Insertion.Bottom, if you are inserting more than one new element you may need to store a reference to the old lastChild and then call Element.next on it. var oldLast = element.lastChild; new Insertion.Bottom(element,content); //or new Insertion.After(oldLast,content); var firstNew = Element.next(oldLast); All of the other cases should be very straightforward. E.g. if Top use down, if After use next, if Before use previous Colin Daniel Eben Elmore wrote:> I''m using the Insertion object and need to get the element just inserted. > The object appears to just return the parent object used. How can I getthe> new element? > > > Thanks! > Daniel > > > > > > . > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Feb 26, 4:29 am, Daniel Eben Elmore <danielelm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m primarily using Insertion.Bottom and don''t see how to use the traversal > methods because I don''t know when I''ve reached the last element. I really > don''t know anything about the element being inserted. It''s an HTML snippet > returned from the server. Any ideas?Browsers conforming to the W3C DOM 2 Core spec will provide a lastChild property that references the last child node: <URL: http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-1451460987>-- Rob --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
If you are always inserting only one sibling like so: var content = ''<div>I am a sibling<span>I am the sibling''s child so I don''t matter</span></div>''; new Insertion.Bottom(element,content); Then it is ok to use element.lastChild: var newEl = element.lastChild; //the div you inserted above However, if you have whitespace or text not wrapped in the node in your insertion content, this will not work. The safer method would be as in my previous example: var oldLast = element.lastChild; //you don''t care what this is, just stored for later reference new Insertion.Bottom(element,content); var newEl = Element.next(oldLast); //gets the div you inserted If the content you are inserting has multiple siblings, then the above code would work to get the first element. TO get subsequent elements just keep using Element.next: //do something with the newEl from above (first element inserted) while(newEl = Element.next(newEl)){ //do something with each additional sibling element inserted } One caveat, if element is completely empty then storing oldLast will not work because there is no lastChild. Could be solved like so: if(!element.lastChild) element.appendChild(document.createTextNode()); var oldLast = element.lastChild; //now oldLast is guaranteed to exist .... Or, if inserting one extraneous empty text node is something you have problems with there are other solutions which I''ll leave up to those who care. <-(my disclaimer in case someone decides to pipe in their objections) Colin Daniel Eben Elmore wrote:> I''m primarily using Insertion.Bottom and don''t see how to use the traversal > methods because I don''t know when I''ve reached the last element. I really > don''t know anything about the element being inserted. It''s an HTML snippet > returned from the server. Any ideas? >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks Collin, extremely helpful. =) -----Original Message----- From: rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Colin Mollenhour Sent: Sunday, February 25, 2007 6:47 PM To: rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Subject: [Rails-spinoffs] Re: Get Newly Inserted Element If you are always inserting only one sibling like so: var content = ''<div>I am a sibling<span>I am the sibling''s child so I don''t matter</span></div>''; new Insertion.Bottom(element,content); Then it is ok to use element.lastChild: var newEl = element.lastChild; //the div you inserted above However, if you have whitespace or text not wrapped in the node in your insertion content, this will not work. The safer method would be as in my previous example: var oldLast = element.lastChild; //you don''t care what this is, just stored for later reference new Insertion.Bottom(element,content); var newEl = Element.next(oldLast); //gets the div you inserted If the content you are inserting has multiple siblings, then the above code would work to get the first element. TO get subsequent elements just keep using Element.next: //do something with the newEl from above (first element inserted) while(newEl = Element.next(newEl)){ //do something with each additional sibling element inserted } One caveat, if element is completely empty then storing oldLast will not work because there is no lastChild. Could be solved like so: if(!element.lastChild) element.appendChild(document.createTextNode()); var oldLast = element.lastChild; //now oldLast is guaranteed to exist .... Or, if inserting one extraneous empty text node is something you have problems with there are other solutions which I''ll leave up to those who care. <-(my disclaimer in case someone decides to pipe in their objections) Colin Daniel Eben Elmore wrote:> I''m primarily using Insertion.Bottom and don''t see how to use thetraversal> methods because I don''t know when I''ve reached the last element. I really > don''t know anything about the element being inserted. It''s an HTML snippet > returned from the server. Any ideas? >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I must not be seeing something simple here: var oldLast = $(''websites-data'').lastChild; console.info(oldLast); new Insertion.Bottom(''websites-data'',transport.responseText); var newLast = oldLast.next(); console.info(newLast); The first console log returns a whitespace text node: "\n \n " The second console log is never recorded and the newLast variable is unusable by my tests. A new element is being inserted, I can see it in the DOM inspector. It looks like the next() method doesn''t like moving off of a whitespace node. -----Original Message----- From: rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Colin Mollenhour Sent: Sunday, February 25, 2007 6:47 PM To: rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Subject: [Rails-spinoffs] Re: Get Newly Inserted Element If you are always inserting only one sibling like so: var content = ''<div>I am a sibling<span>I am the sibling''s child so I don''t matter</span></div>''; new Insertion.Bottom(element,content); Then it is ok to use element.lastChild: var newEl = element.lastChild; //the div you inserted above However, if you have whitespace or text not wrapped in the node in your insertion content, this will not work. The safer method would be as in my previous example: var oldLast = element.lastChild; //you don''t care what this is, just stored for later reference new Insertion.Bottom(element,content); var newEl = Element.next(oldLast); //gets the div you inserted If the content you are inserting has multiple siblings, then the above code would work to get the first element. TO get subsequent elements just keep using Element.next: //do something with the newEl from above (first element inserted) while(newEl = Element.next(newEl)){ //do something with each additional sibling element inserted } One caveat, if element is completely empty then storing oldLast will not work because there is no lastChild. Could be solved like so: if(!element.lastChild) element.appendChild(document.createTextNode()); var oldLast = element.lastChild; //now oldLast is guaranteed to exist .... Or, if inserting one extraneous empty text node is something you have problems with there are other solutions which I''ll leave up to those who care. <-(my disclaimer in case someone decides to pipe in their objections) Colin Daniel Eben Elmore wrote:> I''m primarily using Insertion.Bottom and don''t see how to use thetraversal> methods because I don''t know when I''ve reached the last element. I really > don''t know anything about the element being inserted. It''s an HTML snippet > returned from the server. Any ideas? >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type"> </head> <body bgcolor="#ffffff" text="#000000"> Ahh! Two problems..<br> <br> One, oldLast.next() will not work because you never extended oldLast. Even if you wrapped that in $(), it wouldn''t work if oldLast was a text node because $() and Object.extend don''t extend text nodes.<br> <br> Two, since Prototype doesn''t extend text nodes, and the dom traversal functions call methods using the element extensions, it wouldn''t work with text nodes anyway..<br> <br> next: function(element, expression, index) {<br> return Selector.findElement($(element).nextSiblings(), expression, index);<br> },<br> nextSiblings: function(element) {<br> return $(element).recursivelyCollect(''nextSibling'');<br> }<br> <br> So my example code *would* work if those were written without the extension shortcuts like so:<br> <br> next: function(element, expression, index) {<br> return Selector.findElement(Element.nextSiblings(element), expression, index);<br> },<br> nextSiblings: function(element) {<br> return Element.recursivelyCollect(element,''nextSibling'');<br> }<br> <br> I believe these should all be changed to not use the shortcuts to make them work in as many cases as possible, but I''ve had enough frustration as of late trying to get one patch accepted that I''m not up to submitting another one..<br> <br> In the meantime, use this in place of Element.next(oldLast):<br> <br> var newLast = Element.recursivelyCollect(oldLast,''nextSibling'')[0];<br> <br> Colin<br> <br> <br> Daniel Eben Elmore wrote: <blockquote cite="mid:000f01c759e5$a7497320$6401a8c0@danieloffice" type="cite"> <pre wrap="">I must not be seeing something simple here: var oldLast = $(''websites-data'').lastChild; console.info(oldLast); new Insertion.Bottom(''websites-data'',transport.responseText); var newLast = oldLast.next(); console.info(newLast); The first console log returns a whitespace text node: "\n \n " The second console log is never recorded and the newLast variable is unusable by my tests. A new element is being inserted, I can see it in the DOM inspector. It looks like the next() method doesn''t like moving off of a whitespace node. -----Original Message----- From: <a class="moz-txt-link-abbreviated" href="mailto:rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org">rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org</a> [<a class="moz-txt-link-freetext" href="mailto:rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org">mailto:rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org</a>] On Behalf Of Colin Mollenhour Sent: Sunday, February 25, 2007 6:47 PM To: <a class="moz-txt-link-abbreviated" href="mailto:rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org">rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org</a> Subject: [Rails-spinoffs] Re: Get Newly Inserted Element If you are always inserting only one sibling like so: var content = ''<div>I am a sibling<span>I am the sibling''s child so I don''t matter</span></div>''; new Insertion.Bottom(element,content); Then it is ok to use element.lastChild: var newEl = element.lastChild; //the div you inserted above However, if you have whitespace or text not wrapped in the node in your insertion content, this will not work. The safer method would be as in my previous example: var oldLast = element.lastChild; //you don''t care what this is, just stored for later reference new Insertion.Bottom(element,content); var newEl = Element.next(oldLast); //gets the div you inserted If the content you are inserting has multiple siblings, then the above code would work to get the first element. TO get subsequent elements just keep using Element.next: //do something with the newEl from above (first element inserted) while(newEl = Element.next(newEl)){ //do something with each additional sibling element inserted } One caveat, if element is completely empty then storing oldLast will not work because there is no lastChild. Could be solved like so: if(!element.lastChild) element.appendChild(document.createTextNode()); var oldLast = element.lastChild; //now oldLast is guaranteed to exist .... Or, if inserting one extraneous empty text node is something you have problems with there are other solutions which I''ll leave up to those who care. <-(my disclaimer in case someone decides to pipe in their objections) Colin Daniel Eben Elmore wrote: </pre> <blockquote type="cite"> <pre wrap="">I''m primarily using Insertion.Bottom and don''t see how to use the </pre> </blockquote> <pre wrap=""><!---->traversal </pre> <blockquote type="cite"> <pre wrap="">methods because I don''t know when I''ve reached the last element. I really don''t know anything about the element being inserted. It''s an HTML snippet returned from the server. Any ideas? </pre> </blockquote> <pre wrap=""><!----> . </pre> </blockquote> <br> --~--~---------~--~----~------------~-------~--~----~<br> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. <br> To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org <br> To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org <br> For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en <br> -~----------~----~----~----~------~----~------~--~---<br> </body> </html> <br>
On Feb 27, 9:04 am, Colin Mollenhour <eliteii...-NPSFNn/7+NYVo650/ln6uw@public.gmane.org> wrote:> Ahh! Two problems.. > One, oldLast.next() will not work because you never extended oldLast. Even if you wrapped that in $(), it wouldn''t work if oldLast was a text node because $() and Object.extend don''t extend text nodes.The OP could use: var nextNode = oldLast.nextSibling; unless innerHTML is being used to insert the new nodes - using DOM methods to create nodes will ensure that elements (including next siblings) exist before processing continues, innerHTML will not. -- Rob --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> var newLast = Element.recursivelyCollect(oldLast,''nextSibling'')[0];That did the trick. We sure do need an Element.bottom method, huh ;) -Daniel -----Original Message----- From: rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Colin Mollenhour Sent: Monday, February 26, 2007 5:05 PM To: rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Subject: [Rails-spinoffs] Re: Get Newly Inserted Element Ahh! Two problems.. One, oldLast.next() will not work because you never extended oldLast. Even if you wrapped that in $(), it wouldn''t work if oldLast was a text node because $() and Object.extend don''t extend text nodes. Two, since Prototype doesn''t extend text nodes, and the dom traversal functions call methods using the element extensions, it wouldn''t work with text nodes anyway.. next: function(element, expression, index) { return Selector.findElement($(element).nextSiblings(), expression, index); }, nextSiblings: function(element) { return $(element).recursivelyCollect(''nextSibling''); } So my example code *would* work if those were written without the extension shortcuts like so: next: function(element, expression, index) { return Selector.findElement(Element.nextSiblings(element), expression, index); }, nextSiblings: function(element) { return Element.recursivelyCollect(element,''nextSibling''); } I believe these should all be changed to not use the shortcuts to make them work in as many cases as possible, but I''ve had enough frustration as of late trying to get one patch accepted that I''m not up to submitting another one.. In the meantime, use this in place of Element.next(oldLast): var newLast = Element.recursivelyCollect(oldLast,''nextSibling'')[0]; Colin Daniel Eben Elmore wrote: I must not be seeing something simple here: var oldLast = $(''websites-data'').lastChild; console.info(oldLast); new Insertion.Bottom(''websites-data'',transport.responseText); var newLast = oldLast.next(); console.info(newLast); The first console log returns a whitespace text node: "\n \n " The second console log is never recorded and the newLast variable is unusable by my tests. A new element is being inserted, I can see it in the DOM inspector. It looks like the next() method doesn''t like moving off of a whitespace node. -----Original Message----- From: rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Colin Mollenhour Sent: Sunday, February 25, 2007 6:47 PM To: rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Subject: [Rails-spinoffs] Re: Get Newly Inserted Element If you are always inserting only one sibling like so: var content = ''<div>I am a sibling<span>I am the sibling''s child so I don''t matter</span></div>''; new Insertion.Bottom(element,content); Then it is ok to use element.lastChild: var newEl = element.lastChild; //the div you inserted above However, if you have whitespace or text not wrapped in the node in your insertion content, this will not work. The safer method would be as in my previous example: var oldLast = element.lastChild; //you don''t care what this is, just stored for later reference new Insertion.Bottom(element,content); var newEl = Element.next(oldLast); //gets the div you inserted If the content you are inserting has multiple siblings, then the above code would work to get the first element. TO get subsequent elements just keep using Element.next: //do something with the newEl from above (first element inserted) while(newEl = Element.next(newEl)){ //do something with each additional sibling element inserted } One caveat, if element is completely empty then storing oldLast will not work because there is no lastChild. Could be solved like so: if(!element.lastChild) element.appendChild(document.createTextNode()); var oldLast = element.lastChild; //now oldLast is guaranteed to exist .... Or, if inserting one extraneous empty text node is something you have problems with there are other solutions which I''ll leave up to those who care. <-(my disclaimer in case someone decides to pipe in their objections) Colin Daniel Eben Elmore wrote: I''m primarily using Insertion.Bottom and don''t see how to use the traversal methods because I don''t know when I''ve reached the last element. I really don''t know anything about the element being inserted. It''s an HTML snippet returned from the server. Any ideas? . --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---