How to find the parent class name with known class in jQuery ?
Given an HTML document and the task is to get the parent class of an element with the help of given class name.
Approach 1:
- The on() method is used to select the event handler for selected elements. It means when the user clicks the button then the function call.
- The closest() method is used to return the first ancestor of the selected elements.
- Check if ancestor (parent) class exist then it returns the class name otherwise returns not exist.
Example: This example uses the closest() method to get the first matched ancestor of the element.
<!DOCTYPE html> <html> <head> <title> How to find a parent class name with a known class </title> <script src= </script> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style= "font-size: 17px; font-weight: bold;"> </p> <div class="parent"> <div class="child"></div> </div> <button> click here </button> <p id="GFG_DOWN" style= "color: green; font-size: 24px; font-weight: bold;"> </p> <script> $('#GFG_UP').text('Click on the button to see result'); $('button').on('click', function() { var object = $('.child').closest('.parent'); if (object.length) { $('#GFG_DOWN').text("className = '.child'" + " with parentName = '.parent'" + " Exists"); } else { $('#GFG_DOWN').text("Not Exists"); } }); </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking the button:
-
After clicking the button:
Approach 2:
- The on() method is used to select the event handler for selected elements. It means when the user clicks the button then the function call.
- The parent() method is used to return all ancestor of the selected elements.
- Check if ancestor (parent) class exist then it returns the class name otherwise returns not exist.
Example: This example uses the parents() method to get the all matched ancestors of the element.
<!DOCTYPE html> <html> <head> <title> How to find a parent class name with a known class </title> <script src= </script> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style= "font-size: 17px; font-weight: bold;"> </p> <div class="parent"> <div class="child"></div> </div> <button> click here </button> <p id="GFG_DOWN" style= "color: green; font-size: 24px; font-weight: bold;"> </p> <script> $('#GFG_UP').text('Click on the button to see result'); $('button').on('click', function() { var object = $('.child').parents('.parent'); if (object.length) { $('#GFG_DOWN').text("className = '.child'" + " with parentName = '.parent'" + " Exists"); } else { $('#GFG_DOWN').text("Not Exists"); } }); </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking the button:
-
After clicking the button:
Recommended Posts:
- jQuery | .class Selector
- Not class selector in jQuery
- How to check an element contains a class using jQuery?
- How to select all elements without a given class using jQuery ?
- How to override the CSS properties of a class using another CSS class ?
- jQuery | :parent Selector
- jQuery | parent > child Selector
- JQuery | Get the n-th level parent of an element
- jQuery | parent descendant Selector
- jQuery | parent() & parents() with Examples
- How to remove parent element except its child element using jQuery ?
- CSS | Class Selector
- TypeScript class
- How to exclude particular class name from CSS selector ?
- How to add a class to DOM element in JavaScript?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

