Check if a div does NOT exist with javascript Check if a div does NOT exist with javascript javascript javascript

Check if a div does NOT exist with javascript


var myElem = document.getElementById('myElementId');if (myElem === null) alert('does not exist!');


if (!document.getElementById("given-id")) {//It does not exist}

The statement document.getElementById("given-id") returns null if an element with given-id doesn't exist, and null is falsy meaning that it translates to false when evaluated in an if-statement. (other falsy values)


Check both my JavaScript and JQuery code :

JavaScript:

if (!document.getElementById('MyElementId')){    alert('Does not exist!');}

JQuery:

if (!$("#MyElementId").length){    alert('Does not exist!');}