All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

jQuery Find By ID

Last Updated : Mar 11, 2024

jQuery Find By ID

In this article we will show you the solution of jQuery find by id, here we need to check length of specific element’s id by using length statement.

The length statement is used to count the number of elements in the jquery object.

So it will checks selected element’s id in html document then using return statement we can easily finds out element existence.

Step By Step Guide On jQuery Find By Id :-

Here we defined div element with id attribute and button ‘Find’ with class attribute.

In script block we defined ready() method which is automatically starts loading code of within this function when this program opened on browser and we waiting for click event occurrence which is added on button ‘f’.

Within this using if condition we checks specific elements id length whether 0 or not then we showing respective message on alertbox.

<!DOCTYPE html>
<html>
    <head>
        <title>FIND BY ID</title>
        <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
        <script>
            $(document).ready(function(){
                $('.f').click(function(){
                    if($("#DIVele").length==0){
                        alert("Not Exists");
                    }
                    else{
                        alert("Exist ID=DIVele");
                    }
                });
            })
        </script>
        </head>
        <body>
            <div id="DIVele"></div>
            <button class="f">Find</button>
        </body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Here we imported open source jquery library support file which is needed for coding using jquery in program.
  5. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  6. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  7. In script we defined ready() method within that we added click event with button by using class attribute ‘f’.
  8. Within function we defined if condition, here we specified div element’s id ‘DIVele’ then which is appends with length property for checks whether element id length equal to ‘0’. If return true then that id not present in html and shows message ‘Not Exists’ on alert box.
  9. Otherwise else condition shows message ‘Exist ID=DIVele’ on alert box.
  10. In html block we defined div element with id attribute ‘DIVele’ and button ‘Find’ with different class attribute ‘f’.
  11. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to find element by id in jquery.

Before execution of program we needs to confirm internet connection because then only that jquery file will supports defined jquery codes without error.

When we executes program on browser we can see button ‘Find’ user needs to click on it then respective alert message will appear on alert box.

I hope this article on jQuery find by id helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪