Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a div does NOT exist with javascript

Checking if a div exists is fairly simple

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

But how can I check if a div with the given id does not exist?

like image 284
Wilson Avatar asked Jun 04 '12 18:06

Wilson


People also ask

How do I check if an element ID exists?

Approach 1: First, we will use document. getElementById() to get the ID and store the ID into a variable. Then compare the element (variable that store ID) with 'null' and identify whether the element exists or not.

How do you check if an element is not present in a playwright?

To Fix check if an element exists on the page in Playwright. js, try { await page. waitForSelector(selector, { timeout: 5000 }) // ...`enter code here` } catch (error) {`enter code here` console. log("The element didn't appear.") }

How do I know if a div is selected?

Use the tagName property to check if an element is a select dropdown, e.g. if (select. tagName === 'SELECT') {} .


2 Answers

var myElem = document.getElementById('myElementId'); if (myElem === null) alert('does not exist!'); 
like image 189
Jimbo Jonny Avatar answered Sep 24 '22 22:09

Jimbo Jonny


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)

like image 39
Esailija Avatar answered Sep 26 '22 22:09

Esailija