Recently, I encountered a seemingly simple issue with JavaScript that has me stumped. In my code, I have a variable that stores nodes with a specific class. My goal is to remove these nodes from the DOM while still retaining them in the variable. However, it seems that due to variables storing references, once the nodes are deleted, the variable ends up empty.
Let's take a look at the code snippet:
const modules = document.getElementsByClassName("drag-container");
var parent = document.getElementsByClassName("drag-container")[0].parentElement;
while (parent.firstChild) {
parent.removeChild(parent.lastChild);
}
// At this point, the modules variable is empty
Is there a way to preserve the content of the modules variable even after the nodes have been removed?