Forum: Rails Spinoffs (closed, excessive spam) Clone HTML

Posted by dhjapan (Guest)
on 2008-06-26 18:59
(Received via mailing list)
Hi,
I need to find a way to clone an all form and its content using the
prototype.js framework.
I've found many scripts like this one :

<script type="text/javascript">
Element.addMethods({
  clone: function(element) {
    var newElm = $($(element).cloneNode(true));
  return newElm;
  }
});
</script>

But it seems that I can only clone one element at a time.
What about if I need to clone my form element and all it's
childNodes ?
Thanks a lot for your help.


P.S. : I tried to make a recursive function but can't make it to work:

superClone: function(element) {
  var newElm = $($(element).cloneNode(true));
  if ($(element).hasChildNodes()){
    $A($
(element).childNodes).each(newElm.appendChild(Element.clone()));
    //$A($(element).childNodes).each(Element.remove);
  }
  return newElm;
}
Posted by Dan Dorman (Guest)
on 2008-06-26 19:28
(Received via mailing list)
On Thu, Jun 26, 2008 at 10:58 AM, dhjapan <Leon.Ronssin@gmail.com> 
wrote:
>
> Hi,
> I need to find a way to clone an all form and its content using the
> prototype.js framework.

Does it have to be Prototype? The DOM cloneNode function could very well 
work:

$('el').cloneNode(true); // the "true" parameter copies child elements 
as well

More here: http://developer.mozilla.org/en/docs/DOM:element.cloneNode

:Dan
Posted by dhjapan (Guest)
on 2008-06-26 20:16
(Received via mailing list)
My bad,
.cloneNode(true) works really fine.
I actually mad a big mistake : a few lines above I was emptying the
cloned node instead of the one I wanted to clone my form into.

Thanks a lot and sorry I've wasted your time.

dhjapan
Posted by Frederick Polgardy (Guest)
on 2008-06-26 21:13
(Received via mailing list)
FYI if you're going to insert the result of cloneNode() into your 
document,
make sure none of the elements under the original node has an ID.  The 
IDs
will be duplicated (I think) and $() will start getting all confused.

-Fred

On Thu, Jun 26, 2008 at 1:16 PM, dhjapan <Leon.Ronssin@gmail.com> wrote:

>
> My bad,
> .cloneNode(true) works really fine.
> I actually mad a big mistake : a few lines above I was emptying the
> cloned node instead of the one I wanted to clone my form into.
>
> Thanks a lot and sorry I've wasted your time.
>
> dhjapan


It's not a waste of time as long as you learn something. :)

--
Science answers questions; philosophy questions answers.
This topic is locked and can not be replied to.