How to Check an Element with Specific ID Exists using JavaScript ? Last Updated : 23 Aug, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an HTML document containing some elements and the elements contain some id attribute. The task is to check whether the element with a specific ID exists or not using JavaScript. Below are the approaches to check an element with specific ID exists or not using JavaScript: Table of ContentApproach 1: Using the document.getElementById() methodApproach 2: Using document.getElementById() and JSON.stringify() methodApproach 3: Using document.querySelector() MethodApproach 1: Using the document.getElementById() methodFirst, we will use document.getElementById() to get the ID and store the ID into a variable. Then compare the element (a variable that stores ID) with 'null' and identify whether the element exists or not.Example: This example shows the implementation of the above-explained approach. html <!DOCTYPE html> <html lang="en"> <head> <title> How to Check an Element with Specific ID Exists using JavaScript ? </title> </head> <body> <h1 style="color:green;"> GeeksforGeeks </h1> <p> Click on button to check if element exists using JavaScript </p> <button onClick="GFG_Fun()"> click here </button> <p id="result"></p> <script> let res = document.getElementById('result'); function GFG_Fun() { let el = document.getElementById('GFG'); if (el != null) { res.innerHTML = "Element Exists"; } else { res.innerHTML = "Element Not Exists"; } } </script> </body> </html> Output: Approach 2: Using document.getElementById() and JSON.stringify() methodFirst, we will use document.getElementById() method to get the ID and store the ID into a variable. Then use JSON.stringify() method on the element (variable that store ID) and compare the element with 'null' string and then identify whether the element exists or not.Example: This example shows the implementation of the above-explained approach. html <!DOCTYPE html> <html lang="en"> <head> <title> How to Check an Element with Specific ID Exists using JavaScript ? </title> </head> <body> <h1 style="color:green;"> GeeksforGeeks </h1> <p> Click on button to check if element exists using JavaScript </p> <button onClick="GFG_Fun()"> click here </button> <p id="result"></p> <script> let res = document.getElementById('result'); function GFG_Fun() { let elm = document.getElementById('GFGUP'); if (JSON.stringify(elm) !== "null") { res.innerHTML = "Element Exists"; } else { res.innerHTML = "Element Not Exists"; } } </script> </body> </html> Output: Approach 3: Using document.querySelector() MethodThe document.querySelector() method returns the first element that matches a specified CSS selector. If no elements match, it returns null. This method is more versatile than getElementById() because it allows for more complex selectors (e.g., classes, attributes).Example: HTML <!DOCTYPE html> <html lang="en"> <head> <title>Check if an Element with Specific ID Exists</title> </head> <body> <h1 style="color:green;">GeeksforGeeks</h1> <p>Click on the button to check if the element exists using JavaScript</p> <button onClick="checkElement()">Click here</button> <p id="result"></p> <script> let result = document.getElementById('result'); function checkElement() { let element = document.querySelector('#GFG'); if (element) { result.innerHTML = "Element Exists"; } else { result.innerHTML = "Element Not Exists"; } } </script> </body> </html> Output:Check if an Element with Specific ID Exists Comment More infoAdvertise with us Next Article How to Check if element exists in the visible DOM in JavaScript ? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to Check if a Specific Element Exists in a Set in JavaScript ? To check if a specific element exists in a Set in JavaScript, you can use the has method. The has method returns a boolean indicating whether an element with the specified value exists in the Set Syntax:myset.has(value);Parameters:value: It is the value of the element that has to be checked if it ex 1 min read How to find element with specific ID using jQuery ? The question is to determine whether an element with a specific id exists or not using JQuery. jQuery on() Method: This method adds one or more event handlers for the selected elements and child elements.Syntax: $(selector).on(event, childSelector, data, function, map)Parameters: event: It is requir 3 min read How to check whether an object exists in javascript ? Checking whether an object exists in JavaScript refers to determining if a variable or property has been declared and contains a valid value. This helps avoid errors when accessing or manipulating objects that may be undefined, null, or not initialized properly.Here we have some common approaches to 3 min read How to Check if element exists in the visible DOM in JavaScript ? This article will show you how to check whether an element exists in the visible DOM or not. For that purpose, there are several methods used but we're going to look at a few of them. Example 1: In this example, the element is searched by document.getElementById('Id') and !! operator is used before 2 min read How to Change the ID of Element using JavaScript? We are given an element and the task is to change the ID of elements using JavaScript. ID is unique for any element and it can be assigned to an element only once. JavaScript provides a method to access this id and also to manipulate the id. Syntax:Selected_element.id = newID;Below are the appraoche 2 min read How to Check if an Element Exists in an Array in JavaScript? Given an array, the task is to check whether an element present in an array or not in JavaScript. If the element present in array, then it returns true, otherwise returns false.The indexOf() method returns the index of first occurance of element in an array, and -1 of the element not found in array. 2 min read Like