All TalkersCode Topics

Follow TalkersCode On Social Media

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

jQuery Find By Class

Last Updated : Mar 11, 2024

jQuery Find By Class

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

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

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

Step By Step Guide On jQuery Find By Class :-

Here we defined div element with class attribute and button ‘Find Class’ 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 ‘fc’.

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

<!DOCTYPE html>
<html>
    <head>
        <title>FIND BY CLASS</title>
        <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
        <script>
            $(document).ready(function(){
                $('.fc').click(function(){
                    if($(".DIVelement").length==0){
                        alert("Not Exists");
                    }
                    else{
                        alert("Exist Class=DIVelement");
                    }
                });
            })
        </script>
        </head>
        <body>
            <div class="DIVelement"></div>
            <button class="fc">Find Class</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 ‘fc’.
  8. Within function we defined if condition, here we specified div element’s class ‘DIVelement’ then which is appends with length property for checks whether element class length equal to ‘0’. If return true then that class not present in html and shows message ‘Not Exists’ on alert box.
  9. Otherwise else condition shows message ‘Exist Class=DIVelement’ on alert box.
  10. In html block we defined div element with class attribute ‘DIVelement’ and button ‘Find Class’ with different class attribute ‘fc’.
  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 class name 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 Class’ user needs to click on it then respective alert message will appear on alert box.

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

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪