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>
var n = document.getElementById('test');
var x = n;

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

delete n;

// End modifying any code

alert(x);      // I want this to be null or undefined
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jens FiedererTest Developer/ValidatorCommented:
You can not get x to be null or undefined without changing the value of x.

Jens FiedererTest Developer/ValidatorCommented:
x = null;

or allowing x to go out of scope should work.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jens FiedererTest Developer/ValidatorCommented:
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!
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ZvonkoSystems architectCommented:


But that works only in IE.

Jens FiedererTest Developer/ValidatorCommented:

cannot and should not have any influence on the contents of "x".
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.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.