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.