• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 666
  • Last Modified:

Deleting a DOM Node

I have a script that replaces document.getElementById with a caching version of that.  Everything works fine except that when I delete a node then try to access it again I get a reference to the "Dead" node.

So my question is: how do I actually DELETE a DOM node?  "obj.parentNode.removeChild(obj)" simply removes it from the document and "delete obj" doesn't work.

Any ideas?



<div id="test">test</div>
<script>
var n = document.getElementById('test');
var x = n;

// Start Modifying any code.  do NOT change x itself

n.parentNode.removeChild(n);
delete n;

// End modifying any code

alert(x);      // I want this to be null or undefined
</script>
0
cwolves
Asked:
cwolves
  • 4
  • 2
1 Solution
 
Jens FiedererCommented:
You can not get x to be null or undefined without changing the value of x.

0
 
Jens FiedererCommented:
So
x = null;

or allowing x to go out of scope should work.
0
 
Jens FiedererCommented:
This is a side effect of Javascripts automatic memory management.

Once the node is removed from the document, and can no longer be reached from any variable, the garbage collector can reclaim the storage of the node.

As long as you keep a pointer to the node in x, javascript is NOT ALLOWED to really dispose of the node!
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
ZvonkoSystems architectCommented:
Try:

    obj.removeNode(true);

But that works only in IE.


0
 
Jens FiedererCommented:
obj.removeNode(true);

cannot and should not have any influence on the contents of "x".
0
 
ZvonkoSystems architectCommented:
Sorry, you are true.
I focused on DOM and the allover innerHTML content.
But that is the side effect of GarbagaCollection: as long as ONE reference to a storage object is alive, that long is that Object NOT discarded!
I was aware of that, but not to the final consequencies.

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now