Thanks Collin, extremely helpful. =)

-----Original Message-----
From: [email protected]
[mailto:[EMAIL PROTECTED] On Behalf Of Colin Mollenhour
Sent: Sunday, February 25, 2007 6:47 PM
To: [email protected]
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 [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to