Why does cloneNode exclude custom properties?

This is due to the javascript cloneNode issue and properties .

I see the same behavior. Node.cloneNode does not copy any properties that I add (code from the original message):

    var theSource = document.getElementById("someDiv")
    theSource.dictator = "stalin";

    var theClone = theSource.cloneNode(true);
    alert(theClone.dictator); 

theClone does not contain any dictator property.

I could not find an explanation of why this is so. The documentation in MDN claims to cloneNode“copy all its attributes and their values,” a string that is taken directly from the DOM specification itself .

This seems broken to me because it makes it impossible to make a deep copy of the DOM tree containing custom properties.

Did I miss something?

+5
source
3

.

setAttribute() getAttribute().

var theSource = document.getElementById("someDiv")
theSource.setAttribute('dictator','stalin');

var theClone = theSource.cloneNode(true);
alert(theClone.getAttribute('dictator')); 
+7

. , , , , DOM.

, , , (, DOM node), , . , ( jQuery ).

+3

. cloneNode , . :

 var theSource = document.getElementById("someDiv")
 theSource.dictator = "stalin";
 //or better / more cross browser compatible
 theSource.setAttribute('dictator','stalin');

 var theClone = theSource.cloneNode(true);
 alert(theClone.getAttribute('dictator')); //so, use getAttribute 

This may be a problem with the browser with cloning expando properties. I checked testcase (see below) from this rather old bugzilla report . It did not work in Chrome and Firefox (both latest versions).

//code from testcase @ bugzilla
var a = document.createElement("div");      
a.order = 50;      
alert(a.order);      
b = a.cloneNode(true);      
alert(b.order);    
+2
source

All Articles