4

Here's the deal - I've got a load of elements on a page, and I'm using Javascript to remove some of them (this.parentNode.removeChild(this)) which is working great. However, if I have a variable referring to this node, then remove the node, the variable does NOT lose it's value! But if I then try and perform some other actions on this element, I get errors!

Example:

var element = document.getElementById('ooolookatmeimanelement');
element.parentNode.removeChild(element);
alert(element);

I still get "[Object HTMLblahblahblah]" in the alert, rather than null or undefined - anyone got any ideas how I can check to see if the node has been removed? It's probably something really simple that I'm oblivious to!

4 Answers 4

9

If you remove the node, remove the references too. E.g. in your code, assign null to element:

element = null;

Or if this is not possible, you can always check the value of parentNode. It should give null if the element is not part of the DOM:

if(element.parentNode === null) {
    // element is not part of the DOM
}

But this does not make much sense to me (might depend on the context though): If you removed an element, then you know that you have removed it. Why would you do any further operations on it?

Sign up to request clarification or add additional context in comments.

3 Comments

I'm not sure that's possible in the code I've used - the example I gave is a VERY basic example - I'm actually using TinyMCE, and removing a div which contains an instance of tinyMCE, then later on using a for loop to iterate through all the instances, which of course finds one that no longer exists, but it doesn't know that, so it throws errors at me. I'll see if i can tweak the code I use to remove the object, to stop this from happening :)
Using your code and my head (which isn't working much today), I sorted it :) - if(tinymce.editors[i].container.parentNode.parentNode!==null){ // do stuff to the element that definitely exists
Works like a charm. I had to patch a plugin using this. The plugin forgot to remove/check HTML references that it had previously removed, so once you tried to do something with the plugin after that, it threw loads of errors.
7

You can also use document.body.contains(element); or $.contains(body, element).

Checking for the parentNode is not reliable; if what is removed is an ancestor of the element, instead of the element itself, then parentNode will still return the parent, while the element is not in the DOM anymore.

var ancestor = document.querySelector('div');
var ghost = document.querySelector('br');
ancestor.parentNode.removeChild(ancestor);
console.log(ghost.id + ' son of ' + ghost.parentNode.id);
console.log(ghost.id + (document.body.contains(ghost) || ' dont') + ' exists in DOM');
<div id="ancestorToRemove">
    <p id="a bitch">
        <br id="you">
    </p>
</div>

1 Comment

This should be the accepted answer if you don't know how many parentNodes there are
0

.removeChild() only removes the element from the DOM, and not from memory.

Hence it still exists until it's garbage collected. However you're holding a reference to it, so that won't happen.

Comments

0

Another point that may save time if you're needing to run JavaScript triggered by onClick which does something to itself, you can do this:

onClick="if(!document.getElementById(this.id))return false; YOUR CODE HERE"

For me, this came up when I had a DIV with several children, one of which was a "remove this div" button (actually a DIV made to look like a button). When clicked, this 'button' would remove the DIV from the DOM, as expected, but then would call the onClick event of the DIV itself (which no longer exists). This caused problems. By adding this to my DIV's onClick, I prevented running the code which refers to the DIV after its removal.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.