0

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.

4
  • Your variable naming seems odd. Why is the new clone named old_DataRoot? You should probably describe what you're ultimately trying to accomplish. 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. 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); 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> Commented Oct 18, 2012 at 15:10

3 Answers 3

1

If 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 );
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot that worked great. only thing Ive added was // Restore DataRootBackup.parent.removeChild(DataRoot); DataRootBackup.parent.appendChild(DataRootBackup.nodes); DataRoot=DataRootBackup.nodes;
1

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));

Comments

1

Is this what you're trying to do?

old_DataRoot = DataRoot.cloneNode(true);
DataRoot.parentNode.appendChild(old_DataRoot);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.