Reputation: 3165
I feel like I am misunderstanding how .removeChild
works. I have some code here:
//organization is a string value gotten from an Input node
if (organization == '') {
//there is nothing in the organization form
if (orgContainer !== undefined) {
console.log(orgContainer + ' exists and there is nothing in the organization form.');
//remove the orgContainer
orgContainer.parentNode.removeChild(orgContainer);
console.log(orgContainer + 'removed!');
} else {
console.log(orgContainer + ' does not exist and there is nothing in the organization form.')
}
} else {
if (orgContainer !== undefined || orgContainer !== null) {
console.log(orgContainer + ' exists and there is something in the organization form.');
} else {
console.log(orgContainer + ' does not exist and there is something in the organization form.')
}
}
Basically when I check my page after calling orgContainer.parentNode.removeChild(orgContainer)
I get the console log message of orgContainer removed!
BUT when I run the function again it says that orgContainer
still exists even though I removed it with .removeChild
?
Am I misunderstanding how .removeChild
works? My node gets removed from the parent node but does it not actually cease to exist or something? Does it just get deleted from the DOM but not from memory or something?
I get this wonderful console.log message undefined exists and there is something in the organization form.
.. What? How does it exist if it's undefined
? I've read through the documentation on the developer.mozilla.org site but I am still a bit confused. Any help would be appreciated, thanks!
Upvotes: 0
Views: 458
Reputation: 94379
You cannot remove a declared variable. Although it has been removed from DOM, it doesn't mean it will "undeclare" your variable. You can manually set it to undefined
if you want.
orgContainer = undefined;
Upvotes: 2
Reputation: 413996
Your JavaScript variable still has a reference to the node, and nothing in your code attempts to change that. You successfully take the node out of the DOM, but the value of the orgContainer
variable is never changed.
Upvotes: 2