Andi Pavllo
Andi Pavllo

Reputation: 2506

Replace a node with a duplicate using JavaScript

I have a div element and I need to make a "backup" of it, to restore in certain occasions.

To clone the div, I'm using:

savedDiv = originalDiv.cloneNode(true);

And to restore it:

originalDiv.parentNode.replaceChild(savedDiv,originalDiv);

The problem is that I get Uncaught TypeError: Cannot read property 'replaceChild' of null Any elegant way to solve this issue?

Upvotes: 1

Views: 1091

Answers (1)

Andi Pavllo
Andi Pavllo

Reputation: 2506

I found the issue.

I was trying to execute this operation multiple times but it seems that, after the first time, originalDiv get's consumed.

I fixed it by initializing originalDiv again.

Upvotes: 1

Related Questions