[Proto-Scripty] Re: Cloning an HTML Element

2009-01-24 Thread da644

Thanks Rob, that works perfectly.

Kind regards,

Andrew.

On Jan 23, 11:38 pm, RobG rg...@iinet.net.au wrote:
 On Jan 24, 6:57 am, da644 andrewjdi...@gmail.com wrote:

  Hi,

  I have an HTML object that has been creating using some scripting and
  I wish to insert this into to different locations on the page, e.g.

  var newObj = createMyObject();
  $('location1').insert(newObj);
  $('location2').insert(newObj);

  However doing this the object get inserted into location1 and then
  removed from location1 when it is inserted into location2.

 Which is exactly how it is supposed to work.  :-)

  I have
  tried using the clone method:

  var newObj = createMyObject();
  var newObj2 = Object.clone(newObj);

 That is for cloning (more like copying) a native javascript object.
 What you are trying to do is clone a host object.

  $('location1').insert(newObj);
  $('location2').insert(newObj2);

  However this doesn't work. It doesn't error, but it simply stops
  anymore Javascript from executing.

  Is there a way to do what I want using Prototype?

 If your object is an HTML element that implements the DOM Core
 interface Node (and most of them do), use its cloneNode method:

   $('location1').insert(newObj.cloneNode(true));

 It is called cloneNode to ensure it is obvious that its purpose is
 to clone Nodes, not other types of objects.  Be aware however that it
 will clone all of the element's properties, including ones like ID
 that should be unique in the document.

 Also, listeners may or may not be cloned depending on how they were
 added and which browser the code is run in.

 --
 Rob
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



[Proto-Scripty] Re: Cloning an HTML Element

2009-01-23 Thread RobG



On Jan 24, 6:57 am, da644 andrewjdi...@gmail.com wrote:
 Hi,

 I have an HTML object that has been creating using some scripting and
 I wish to insert this into to different locations on the page, e.g.

 var newObj = createMyObject();
 $('location1').insert(newObj);
 $('location2').insert(newObj);

 However doing this the object get inserted into location1 and then
 removed from location1 when it is inserted into location2.

Which is exactly how it is supposed to work.  :-)


 I have
 tried using the clone method:

 var newObj = createMyObject();
 var newObj2 = Object.clone(newObj);

That is for cloning (more like copying) a native javascript object.
What you are trying to do is clone a host object.

 $('location1').insert(newObj);
 $('location2').insert(newObj2);

 However this doesn't work. It doesn't error, but it simply stops
 anymore Javascript from executing.

 Is there a way to do what I want using Prototype?

If your object is an HTML element that implements the DOM Core
interface Node (and most of them do), use its cloneNode method:

  $('location1').insert(newObj.cloneNode(true));

It is called cloneNode to ensure it is obvious that its purpose is
to clone Nodes, not other types of objects.  Be aware however that it
will clone all of the element's properties, including ones like ID
that should be unique in the document.

Also, listeners may or may not be cloned depending on how they were
added and which browser the code is run in.


--
Rob
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---