How to check if the DOM reference has been destroyed?


This question already has an answer here:

  • How to check if element exists in the visible DOM? 12 answers

Say I select an element the old fashioned way (or any other way by which a DOM reference might be obtained):

var el = document.getElementById('myFavoriteElement');

Then I remove that element's parent from the DOM, thus removing el as well. Or I just remove el directly.

Is there a way to check whether el is still a valid reference, whether the HTML to which it refers still exists in the DOM? Something along the lines of el.hasBeenDestroyed as a boolean attribute, or something like that?

The baseURI attribute will be filled in if the element is on the page DOM. Check that.

Alternatively, try document.body.contains(node).