Home > Enterprise >  I keep getting an Uncaught TypeError: theoldone.parentNode is null
I keep getting an Uncaught TypeError: theoldone.parentNode is null

Time:01-24

I am trying to clone an element to reset the animation. I've used this bit of code before and it worked perfectly but now it isn't working I have no idea why not. Here is the code:

var justlike = theoldone.cloneNode(true); theoldone.parentNode.replaceChild(justlike, theoldone);

theoldone is a const for the element I want to clone.

CodePudding user response:

When using .cloneNode() on an element, you only copy its Node. So you cant access its parents since it is a copy and is seperated from its parents.

From Mozilla Wiki:

Return Value

The new Node cloned. The cloned node has no parent and is not part of the document, until it is added to another node that is part of the document, using Node.appendChild() or a similar method.

  •  Tags:  
  • Related