when Im cloning an object in javascript by doing object.cloneNode(true) the parentNode is null in the new copy. Im trying to set it but with no success. my code look like this:
old_DataRoot = DataRoot.cloneNode(true);
old_DataRoot.parentNode=DataRoot.parentNode.cloneNode(true);
also tried:
old_DataRoot = DataRoot.cloneNode(true);
old_DataRoot.parentNode.appendChild(DataRoot.parentNode.cloneNode(true));
both options give me "old_DataRoot.parentNode is null or not an object" what am I doing wrong?
thanks alot, Yoni.
when Im cloning an object in javascript by doing object.cloneNode(true) the parentNode is null in the new copy. Im trying to set it but with no success. my code look like this:
old_DataRoot = DataRoot.cloneNode(true);
old_DataRoot.parentNode=DataRoot.parentNode.cloneNode(true);
also tried:
old_DataRoot = DataRoot.cloneNode(true);
old_DataRoot.parentNode.appendChild(DataRoot.parentNode.cloneNode(true));
both options give me "old_DataRoot.parentNode is null or not an object" what am I doing wrong?
thanks alot, Yoni.
Share Improve this question edited Oct 18, 2012 at 14:54 yoni asked Oct 18, 2012 at 14:41 yoniyoni 471 gold badge2 silver badges8 bronze badges 4-
Your variable naming seems odd. Why is the new clone named
old_DataRoot
? You should probably describe what you're ultimately trying to acplish. – I Hate Lazy Commented Oct 18, 2012 at 14:46 - what Im trying to do is to make a backup of the original DataRoot in order to recover it later. – yoni Commented Oct 18, 2012 at 14:56
-
Why do you need it to have a
.parentNode
? Is it so you remember where to re-insert it? If so, just make a variable reference to the parent.var backup = DataRoot.cloneNode(true); var backup_par = DataRoot.parentNode;
Then later, you can insert the backup.backup_par.appendChild(backup);
or you can do a.replaceChild
from the parent:backup_par.replaceChild(backup, DataRoot);
– I Hate Lazy Commented Oct 18, 2012 at 14:59 -
first of all thank you for your answer. the reason I want to do that is that after I clone the DataRoot I want to perform a
DataRoot.selectSingleNode("//Users/*[@Id = \"" + NodeID + "\"]");
and without a parent it doesnt seem to work. the XML look like this<Users> <User Id="m2" Email="" RealName="m2"/> <User Id="m3" Email="" RealName="m3"/> <User Id="m6" Email="" RealName="m6"/> <UsersSearch SearchText="m1"/> </Users>
– yoni Commented Oct 18, 2012 at 15:10
3 Answers
Reset to default 1If you're trying
to make a backup of the original DataRoot in order to recover it later.
then consider
// Backup
var DataRootBackup = {
nodes: DataRoot.cloneNode(true),
parent: DataRoot.parentNode
};
// Restore
DataRootBackup.parent.appendChild( DataRootBackup.nodes );
Yes, that's true, parentNode
is a read-only property.
In your second case you need know that only one of the nodes is attached to the DOM. It's dataRoot
which still has the parentnode, the result of the clone (which you called old_DataRoot
) is unattached:
dataRoot.parentNode.appendChild(newDataRoot = dataRoot.cloneNode(true));
Is this what you're trying to do?
old_DataRoot = DataRoot.cloneNode(true);
DataRoot.parentNode.appendChild(old_DataRoot);