JavaScript | Check if element exists in the visible DOM
The task is to find whether an element exists in the visible DOM or not. For that purpose, there is a number of methods used but we’re going to look at few of them.
Example-1: In this example, the element is searched by document.getElementById(‘Id’) and !! is used before selector to get the boolean result.
<!DOCTYPE HTML> <html> <head> <title> JavaScript | Check if element exists in the visible DOM. </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> Check if an Element exists in visible DOM </p> <button onclick="gfg_Run()"> check </button> <p id="GFG_DOWN" style="color:green; font-size: 23px; font-weight: bold;"> </p> <script> var el_down = document.getElementById("GFG_DOWN"); var id = 'GFG_UP'; function gfg_Run() { var element = !!document.getElementById(id); var ans = ''; if (element) { ans = "Element of id = '" + id + "' exists in visible DOM."; } else { ans = "Element of id = '" + id + "' not exists in visible DOM."; } el_down.innerHTML = ans; } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example-2: In this example, the element is searched by document.getElementsByClassName(‘className’) and length property is used to check whether variable contains results or not.
<!DOCTYPE HTML> <html> <head> <title> JavaScript | Check if element exists in the visible DOM. </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" class="p" style="font-size: 15px; font-weight: bold;"> Check if an Element exists in visible DOM </p> <button onclick="gfg_Run()"> check </button> <p id="GFG_DOWN" class="p" style="color:green; font-size: 23px; font-weight: bold;"> </p> <script> var el_down = document.getElementById("GFG_DOWN"); var className = 'p'; function gfg_Run() { var element = document.getElementsByClassName(className); var ans = ''; if (element.length > 0) { ans = "Element of className = '" + className + "' exists in visible DOM."; } else { ans = "Element of className = '" + className + "' not exists in visible DOM."; } el_down.innerHTML = ans; } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- How to check if an element is visible in DOM ?
- How to check an element is visible or not using jQuery?
- How to check whether an object exists in javascript ?
- How to check a key exists in JavaScript object?
- JavaScript | Check if a key exists inside a JSON object
- How to check if a scrollbar is visible?
- How to check if an element has any children in JavaScript ?
- Check whether HTML element has scrollbars using JavaScript
- How to Check if an element is a child of a parent using JavaScript?
- jQuery | :visible Selector
- How to add a class to DOM element in JavaScript?
- How to set the value of a select box element using JavaScript?
- How to get the type of DOM element using JavaScript?
- How to remove an HTML element using JavaScript ?
- JavaScript | Adding a class name to the element
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



