Artisan72
Artisan72

Reputation: 3600

How can I check if an element still exists in the DOM?

How can I check if an element still exists in the DOM?

<div id="parent">
  ...
  <div class="my-class"></div>
</div>
let parent = document.getElementById('parent');
let myElement = parent.getElementsByClassName('my-class')[0];
parent.remove();

function stillExists(element) {
   // check if element still exists in DOM
}

stillExists(myElement) // should be false

Upvotes: 1

Views: 2263

Answers (1)

Simply do

document.contains(myElement)

Upvotes: 5

Related Questions