Skip to content Skip to sidebar Skip to footer

Making A Copy Of The Dom

If I am going to change the document, can I make a copy of the old version by something like oldDoc = (copy of document, by value) and then use things like oldDoc.getElementById('m

Solution 1:

Yes, using the function cloneNode like this:

var oldDoc = document.cloneNode(true);

You can read more about this function here. Also, here is a snippet with a small working example:

const docCopy = document.cloneNode(true);
document.querySelector('div').textContent = 'New div content';

console.log(docCopy.querySelector('div').textContent);
console.log(document.querySelector('div').textContent);
<div>Old div content</div>

Post a Comment for "Making A Copy Of The Dom"