All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

jQuery Find Parent With Class

Last Updated : Mar 11, 2024

jQuery Find Parent With Class

In this article we will show you the solution of jQuery find parent with class, here we need to use closest() method and needs to check length of parent element for achieve the result. The closest() method returns the first ancestor of the selected element.

An ancestor is a parent, grandparent, great-grandparent and so on. The length statement is used to count the number of elements in the jquery object.

Step By Step Guide On jQuery Find Parent With Class :-

Here we defined nested div elements with class attributes, button ‘Find Parent’ with class attribute and h3 element.

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 ‘fpc’.

Within this closest method is collecting parent by class name and using if condition we checks element length whether not null or not then we showing respective message.

<!DOCTYPE html>
        <title>FIND PARENT WITH CLASS</title>
        <script src=""></script>
                    var obj=$('.DIVelement').closest('.DIVparent');
                        $('#res').text("Class name='.DIVelement'"+" with Parent='.DIVparent'"+" Exists");
                        $('#res').text("Not Exists");
            <div class="DIVparent">
                <div class="DIVelement"></div>
            <button class="fpc">Find Parent</button>
            <h3 id="res"></h3>
  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 ‘fpc’.
  8. Within function we specifying parent div element on closest() method by using class name ‘.DIVparent’ with child div element’s class ‘DIVelement’ then result stored on variable ‘obj’.
  9. Variable obj is appends with length property for checks whether length equal above than ‘0’. If return true then parent element gets found and appends message of parent class name on h3 element.
  10. Otherwise else condition appends message ‘Not Exists’ on h3 element.
  11. In html block we defined parent, child div elements with respective class attributes ‘DIVparent, DIVelement’, button ‘Find Parent’ with different class attribute ‘fpc’ and h3 element with id ‘res’.
  12. 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 parent element with class 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 Parent’ user needs to click on it then respective message will appear on webpage.

I hope this article on jQuery find parent with class 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 🡪