Open In App

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. 
 

$(selector).on(event, childSelector, data, function, map)
$(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.
 






<!DOCTYPE HTML>
<html>
    <head>
        <title>
            How to find element with
            specific id exists or not
        </title>
         
        <script src =
        </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: 
 



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.
 




<!DOCTYPE HTML>
<html>
    <head>
        <title>
            How to find element with
            specific id exist or not
        </title>
         
        <script src =
        </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: 
 

 


Article Tags :