Check if a div does NOT exist with javascript

前端 未结 9 2205
野性不改
野性不改 2020-12-12 16:42

Checking if a div exists is fairly simple

if(document.getif(document.getElementById(\'if\')){

}

But how can I check if a div with the give

9条回答
  •  无人及你
    2020-12-12 17:35

    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)

提交回复
热议问题