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

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.