Questions tagged [clonenode]

The Node.cloneNode() method returns a duplicate of the node on which this method was called.

From Mozilla Developer Network:

The Node.cloneNode() method returns a duplicate of the node on which this method was called.

Cloning a node copies all of its attributes and their values, including intrinsic (in–line) listeners. It does not copy event listeners added using addEventListener() or those assigned to element properties. (e.g. node.onclick = fn) Moreover, for a element, the painted image is not copied.

The duplicate node returned by cloneNode() is not part of the document until it is added to another node that is part of the document using Node.appendChild() or a similar method. It also has no parent until it is appended to another node.

If deep is set to false, child nodes are not cloned. Any text that the node contains is not cloned either, as it is contained in one or more child Text nodes.

If deep evaluates to true, the whole subtree (including text that may be in child Text nodes) is copied too. For empty nodes (e.g. <img> and <input> elements) it doesn't matter whether deep is set to true or false.

123 questions
19
votes
2 answers

Clone element with all its events

I'm cloning a textarea in a page but the cloned element doesn't have any event of the primary element, is there any way to clone all events in cloned element? var dupNode = node.cloneNode(deep);
ehsandotnet
  • 1,050
  • 3
  • 16
  • 26
8
votes
3 answers

Why does cloneNode exclude custom properties?

This is related to the question javascript cloneNode and properties. I'm seeing the same behaviour. Node.cloneNode does not copy over any properties that I add myself (code from original post): var theSource =…
Scott Cameron
  • 5,293
  • 2
  • 29
  • 32
7
votes
4 answers

cloneNode in internet explorer

While executing the following code IE throws the error -- Object doesn't support this property or method -- referring to the cloneNode() method. 'i' is the loop counter, source and dest are both HTML select…
Mark
  • 715
  • 2
  • 7
  • 9
6
votes
1 answer

$.clone and .cloneNode

I am a bit confused on the difference between jQuery $.clone and the raw .cloneNode property. If I am doing $('blah').cloneNode(true) this will create a global object outside of the jQuery space. If I use $('blah').clone(true) this will create a…
Andy
  • 18,723
  • 12
  • 46
  • 54
5
votes
6 answers

How to Maintain Correct Javascript Event After Using cloneNode(true)

I have a form element that contains multiple lines of inputs. Think of each line as attributes of a new object that I want to create in my web application. And, I want to be able to create multiple new objects in one HTTP POST. I'm using…
newtonapple
  • 4,123
  • 3
  • 33
  • 31
5
votes
3 answers

How efficient is element.cloneNode(true) (deep clone)?

I'm building the HTML code within an XML DOM object to be used as the contents of the innerHTML of a div element using an XSL template. Traditionally we create a new XML DOM document and add the input parameters as XML Elements for the transform via…
JoelB
  • 349
  • 3
  • 6
  • 11
3
votes
3 answers

IE not desired new line on TD dynamically added with Javascript

I wrote a JS script to dynamically add rows on a table. On a select element change, this script reads an HTML div set to display:none and clone this template with CloneNode(True) method. Then, it adds this new object before a placeholder …
sdarjazi
  • 65
  • 4
3
votes
2 answers

Use js cloneNode to copy

I want to use cloneNode and avoid JQuery to copy a html template in DOM, modify the (nested) children and their ids (and append to an element). var clone = itm.cloneNode(true); clone.id = 'c' + tid; //replace with new, unique id itm =…
AlterSchwede
  • 463
  • 1
  • 5
  • 9
3
votes
1 answer

Not cloning as expected

I have a JavaScript code that clones the last li node, if a user clicks on the button Add Choice or if an input clicked is the last input. The cross button on the right removes the li node it is located in. It won't remove if there's only one input…
Zainab Hussain
  • 313
  • 1
  • 10
3
votes
1 answer