How to check if a div does not exist with JavaScript?

Spread the love

To check if a div does not exist with JavaScript, we try to get the element and check if it’s null.

For instance, we write

if (!document.getElementById("given-id")) {
  //...
}

to try to get the element with ID given-id with getElementById.

If it returns null, then the element doesn’t exist.

Leave a Reply

Your email address will not be published. Required fields are marked *