Aha. That makes sense. I was thinking it was doing more of an
innerHTML sort of thing, updating with the actual HTML code, and I
wasn't thinking object-wise.
Walter
On Mar 3, 2010, at 4:03 PM, T.J. Crowder wrote:
Hi,
But can anyone explain what my conceptual failure was here?
Elements can have at most *one* parent element. What you were
expecting would have resulted in an element having two parents, which
the tree-like structure of the DOM does not allow. This isn't a
Prototype thing, it goes at least back to the first formal DOM
specification (and probably farther):
"appendChild: Adds the node newChild to the end of the list of
children of this node. If the newChild is already in the tree, it is
first removed."[1]
Conveniently, though, there's cloneNode[2], which would have done
pretty much what you were expecting:
$('bar').update(this.down('p').cloneNode());
Be careful if you clone a node that has an ID attribute, though; you
have to change the ID before adding it back to the tree (since IDs
*must* be unique).
[1] http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html#method-appendChild
[2] http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html#method-cloneNode
HTH,
--
T.J. Crowder
Independent Software Consultant
tj / crowder software / com
www.crowdersoftware.com
On Mar 3, 8:02 pm, Walter Lee Davis <wa...@wdstudio.com> wrote:
I was trying to do a very simple image-swap effect:
<div id="foo">
<p><img ... /></p>
</div>
<div id="bar">
<p><img ... /></p>
</div>
$('foo').observe('click',function(evt){
$('bar').update(this.down('p'));
});
I expected both #foo and #bar to contain the same image, but instead,
#foo was empty, and #bar contained the image originally in #foo.
I resolve the problem by changing my method from copying the P to
simply making the two images have the same src attribute.
But can anyone explain what my conceptual failure was here? Why did
update() seem to do what I would have expected the following
construction to do:
$('bar').update(this.down('p').remove());
???
Thanks in advance,
Walter
--
You received this message because you are subscribed to the Google
Groups "Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
.
To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com
.
For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en
.
--
You received this message because you are subscribed to the Google Groups "Prototype
& script.aculo.us" group.
To post to this group, send email to prototype-scriptacul...@googlegroups.com.
To unsubscribe from this group, send email to
prototype-scriptaculous+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/prototype-scriptaculous?hl=en.