clonenode

javascript cloneNode and properties

独自空忆成欢 提交于 2019-12-30 15:02:51
问题 Is there a quick way to "super" deep clone a node, including its properties? (and methods, I guess) I've got something like this: var theSource = document.getElementById("someDiv") theSource.dictator = "stalin"; var theClone = theSource.cloneNode(true); alert(theClone.dictator); The new cloned object has no dictator property. Now, say I've got a thousand properties attached to theSource - how can I (non-explicitly) transfer/copy them to the clone? // EDIT @Fabrizio Your hasOwnProperty answer

javascript cloneNode and properties

孤人 提交于 2019-12-30 15:02:31
问题 Is there a quick way to "super" deep clone a node, including its properties? (and methods, I guess) I've got something like this: var theSource = document.getElementById("someDiv") theSource.dictator = "stalin"; var theClone = theSource.cloneNode(true); alert(theClone.dictator); The new cloned object has no dictator property. Now, say I've got a thousand properties attached to theSource - how can I (non-explicitly) transfer/copy them to the clone? // EDIT @Fabrizio Your hasOwnProperty answer

javascript cloneNode and properties

痞子三分冷 提交于 2019-12-30 15:02:12
问题 Is there a quick way to "super" deep clone a node, including its properties? (and methods, I guess) I've got something like this: var theSource = document.getElementById("someDiv") theSource.dictator = "stalin"; var theClone = theSource.cloneNode(true); alert(theClone.dictator); The new cloned object has no dictator property. Now, say I've got a thousand properties attached to theSource - how can I (non-explicitly) transfer/copy them to the clone? // EDIT @Fabrizio Your hasOwnProperty answer

documentFragment.cloneNode(true) doesn't clone jQuery data

纵然是瞬间 提交于 2019-12-24 10:48:07
问题 I have a documentFragment with several child nodes containing some .data() added like so: myDocumentFragment = document.createDocumentFragment(); for(...) { myDocumentFragment.appendChild( $('<a></a>').addClass('button') .attr('href', 'javascript:void(0)') .html('click me') .data('rowData', { 'id': 103, 'test': 'testy' }) .get(0) ); } When I try to append the documentFragment to a div on the page: $('#div').append( myDocumentFragment ); I can access the data just fine: alert( $('#div a:first'

JavaScript: cloneNode vs importNode

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-12 01:37:46
问题 I noticed in some code samples to seemingly different ways to clone a DOM node and append it to an existing element: element.appendChild(something.cloneNode(true)); element.appendChild(document.importNode(something, true)); Both have the effect of copying a node. The second version seems more verbose, and implies that the copy is actually somewhere concrete first, though it still needs to find a proper home. However, it is used by MDN and a few other as an illustration of using the template

Why does cloneNode exclude custom properties?

淺唱寂寞╮ 提交于 2019-12-07 05:08:04
问题 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 = document.getElementById("someDiv") theSource.dictator = "stalin"; var theClone = theSource.cloneNode(true); alert(theClone.dictator); theClone does not contain any property "dictator". I haven't been able to find any explanation for why this is the case. The documentation on MDN states

Cloning elements [div] [script] in Javascript

試著忘記壹切 提交于 2019-12-02 01:59:54
问题 I'm trying to clone a div in pure Javascript, however, cloneNode leads to duplicate ids (div_0). I would like to increment the id as div_1, div_2... and do the same to somevar = {'elem', 'div_1'}... Thanks <html> <head></head> <body> <div id="mydiv"> <div id="div_0"> <script type="text/javascript"> <!-- somevar = {'elem', 'div_0'}; //--> </script> <p>HELLO</p> </div> </div> <a href="#" onclick="cloning()">CLONE</a> <script type="text/javascript"> function cloning() { var container = document

How to dynamically add a cloned node in angular2 (equivalent to cloneNode)

こ雲淡風輕ζ 提交于 2019-12-01 11:13:42
In Angular2, I need to duplicate a node rather than moving it in some cases. That node has angular2 properties so cloneNode doesn't work. How can I do it? *what doesn't work let el = <HTMLElement>document.getElementById(divId); if ((<HTMLElement>el.parentNode).id == 'itsMe') el = <HTMLElement>el.cloneNode(true); document.getElementById(anotherId).appendChild(el); *what would work, from Angular2: Cloning component / HTML element and it's functionality @Component({ selector: 'my-app', template: ` <template #temp> <h1 [ngStyle]="{background: 'green'}">Test</h1> <p *ngIf="bla">Im not visible</p> <

Cloned elements cannot be submitted in Angular4

冷暖自知 提交于 2019-12-01 09:37:09
问题 I have a template with two fields for.eg name and age, that needs to cloned and appended to the same container. I achieved this using the following code. html file <ng-template #tpl> <div class="form-group"> <input type="text" id="name" class="form-control" name="name" ngModel #name="ngModel"> <input type="text" id="age" class="form-control" name="age" ngModel #age="ngModel"> <button type="Button" >Remove</button> </div> </ng-template> <div>Some element</div> <form #myForm="ngForm" novalidate

How to dynamically add a cloned node in angular2 (equivalent to cloneNode)

笑着哭i 提交于 2019-12-01 09:08:19
问题 In Angular2, I need to duplicate a node rather than moving it in some cases. That node has angular2 properties so cloneNode doesn't work. How can I do it? *what doesn't work let el = <HTMLElement>document.getElementById(divId); if ((<HTMLElement>el.parentNode).id == 'itsMe') el = <HTMLElement>el.cloneNode(true); document.getElementById(anotherId).appendChild(el); *what would work, from Angular2: Cloning component / HTML element and it's functionality @Component({ selector: 'my-app', template: