Check if a div does NOT exist with javascript

前端 未结 9 2213
野性不改
野性不改 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:31

    There's an even better solution. You don't even need to check if the element returns null. You can simply do this:

    if (document.getElementById('elementId')) {
      console.log('exists')
    }
    

    That code will only log exists to console if the element actually exists in the DOM.

提交回复
热议问题