Reputation: 3088
I want to remove the temporary element which is created dynamically.
x = document.createElement("DIV");
x.innerHTML = res;
y = x.parentNode;
x = y.removeChild(x);
x = null;
I've written those codes but since it does note have a parent, they dont work. I know i can create a second temp node to keep first one but then i need a third node to delete the second one.... :) Here i need a function to delete the node without needing a parent node. I also think that i can put the node to directly to document object and delete it, but i'm looking for more efficient solutions if exists.
Upvotes: 0
Views: 272
Reputation: 38441
removeChild
is just from removing an element from a document. Since your element isn't part of a document, you don't need to use it. Just make sure that you don't have kept any references to the element in your code, for example by setting the variable(s) to null
just as you are doing, and JavaScript automatic garbage collection will take care of the rest.
If your variable is a local variable in a function, then you don't even need to set it to null
, because it will cease to exist on it's own.
There is no direct way to "delete" an object in JavaScript.
Upvotes: 1
Reputation: 7906
You need not remove the node, since it is not added to the DOM. When you set the variable to null
, the element will cease to exist. In general, DOM nodes will always have a parent, provided they're added to the model, but here you don't do that in your code.
Upvotes: 4