How to find element with specific ID using jQuery ? Last Updated : 26 May, 2021 Comments Improve Suggest changes Like Article Like Report 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 required parameter. It specifies one or more event(s) or namespaces to attach to the selected elements. In case of multiple event values, those are separated by space. Event must be a valid.childSelector: It is optional parameter. It specifies that the event handler should only be attached to the defined child elements.data: It is optional parameter. It specifies additional data to pass to the function.function: It is required parameter. It specifies the function to run when the event occurs.map: It specifies an event map ({event:func(), event:func(), ...}) having one or more event to add to the selected elements, and functions to run when the events happens. jQuery length Property: The length property is used to count number of the elements of the jQuery object.Syntax: $(selector).length Example 1: In this example, the var name contains ID name to check. First JQuery selector checks for the ID via var name and then length property is used to verify whether something is selected or not. html <!DOCTYPE HTML> <html> <head> <title> How to find element with specific id exists or not </title> <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"> </script> </head> <body style = "text-align:center;"> <h1 id = "h" style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;"> </p> <button id = "button"> Click here </button> <p id = "GFG_DOWN" style = "color:green; font-size: 20px; font-weight: bold;"> </p> <script> $('#GFG_UP'). text("Click on button to check existence of particular ID"); var name = "h"; $("button").on('click', function() { var exist = ""; if($("#" + name).length == 0) { exist = "Not Exists"; } else { exist = "Exists"; } $('#GFG_DOWN').text("Element of ID = '" + name + "' " + exist); }); </script> </body> </html> Output: Before clicking on the button: After clicking on the button: Example 2: In this example, the ID name to check is directly passed in the JQuery selector. First JQuery selector checks for the ID and then length property is used to verify whether something is selected or not. html <!DOCTYPE HTML> <html> <head> <title> How to find element with specific id exist or not </title> <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"> </script> </head> <body style = "text-align:center;"> <h1 id = "h" style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;"> </p> <button id = "button"> Click here </button> <p id = "GFG_DOWN" style = "color:green; font-size: 20px; font-weight: bold;"> </p> <script> $('#GFG_UP'). text("Click on button to check existence of particular ID"); $("button").on('click', function() { var exist = ""; if($("#button").length == 0) { exist = "Not Exists"; } else { exist = "Exists"; } $('#GFG_DOWN').text("Element of ID = 'button" + "' " + exist); }); </script> </body> </html> Output: Before clicking on the button: After clicking on the button: Comment More infoAdvertise with us Next Article How to find element with specific ID using jQuery ? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JQuery Similar Reads How to find all children with a specified class using jQuery ? There are lots of javascript libraries like - anime.js, screenfull.js, moment.js, etc. JQuery is also a part of javascript libraries. It is used to simplify the code. It is a lightweight and feature-rich library. In this article, we will learn how to find all children with a specified class of each 2 min read How to find an element by text using jQuery ? We will learn how to find an element using jQuery's API. This article requires some knowledge of HTML, CSS, JavaScript, Bootstrap, and jQuery. The elements can be selected based on whether they contain the string we want to find. This can be done using the jQuery contains selector to select elements 2 min read How to Check an Element with Specific ID Exists using JavaScript ? 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 ContentApproa 3 min read How to change the element id using jQuery ? The jQuery methods are used to change the element ID which are described below: jQuery attr() Method: This method set/return attributes and values of the selected elements. If this method is used to return the attribute value, it returns the value of first selected element. If this method is used to 3 min read How to get focused element using jQuery? To focus on element there is an inbuilt selector available in jQuery, which is used to detect the currently focused element. When the element is get focused by the mouse click or by the tab-navigating button this selector return the selected element. You can add the element in JQuery to focused that 2 min read Like