The JavaScript library, jQuery, provides certain methods that are used to fetch parent elements of an HTML element. Using these methods you can fetch the direct parent, or all parents of an element with great ease. Moreover, fetching elements between two specified elements, or the closest parent element that matches the selected element is also possible using jQuery methods.

This guide will teach you how to use jQuery methods to get parent element. Let’s get started.

How to get Parent Element in jQuery

Four methods are available to fetch the parent element which are as follows.

  1. parent() method
  2. parents() method
  3. parentUntil() method
  4. closest() method

Let’s learn each of the above-mentioned methods in detail.

parent() Method

For the purpose of finding the direct parent of an element, the parent() method is used. It is a built-in jQuery function that only goes one level up the specified element and fetches the immediate parent of that element.

Syntax

$(selector).parent(filter)

Note: The filter parameter is used to compact the search for parent element by specifying a selector expression and it is optional.

Example

Suppose a you want to fetch the direct parent of a element  which is present in an

  • element which further is a part of a
    element.

    HTML

    <div style=“width:500px;”>I am a great-grandparent of span element

        <ul>I am grandparent of span element

            <li>I am direct parent of span element

                <span>I am the span element</span>

            </li>

        </ul>

    </div>

    There are a total of four elements generated in the above code, which are;

    ,

      ,

    • , and . Observing their hierarchy in the above the
    • element is regarded as a direct parent of the element,
        is the grandparent of the element, and

        is the great-grandparent because all of the elements are nested inside the

        element.

        jQuery

        $(document).ready(function(){

            $(“span”).parent().css({“color”: “purple”, “border”: “2px solid purple”});

        });

        We have applied the parent() method on the element and also chained the css() method to it in order to highlight the direct parent of the element and verify that the parent element is accessed successfully.

        Some basic styling is also applied to these elements using CSS for better demonstration and understanding.

        Output

        <img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/image1-44.png" data-lazy- height="313" src="data:image/svg xml,” width=”701″>

        The parent() method is working properly and the parent element is accessed successfully.

        parents() Method

        The parents() method works in a similar way to the parent() method with the only difference that instead of fetching the direct parent it fetches all the parents of the specified element.

        Syntax

        $(selector).parents(filter)

        Note: The filter parameter is used to compact the search for parent element by specifying a selector expression and it is optional.

        Example

        To understand the concept of the parents() method, we will consult the same example as above and use the parents() method instead of the parent() method and see how it works.

        jQuery

        $(document).ready(function(){

            $(“span”).parents().css({“color”: “purple”, “border”: “3px solid purple”});

        });

        The above code should highlight all the parents of the element in the style specified by the css() method.

        Output

        <img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/image3-43.png" data-lazy- height="399" src="data:image/svg xml,” width=”812″>

        The element highlighted above the body is the element. The parents() method fetches it as well since it is also a parent of the specified element.

        parentsUntil() Method

        In order to fetch parent elements between two specified elements, the parentUntil() method is used.

        Syntax

        $(selector).parentsUntil(stop,filter)

        Note: The filter parameter has the same function as that of parent() and parents() method, however, the stop parameter is used to denote the element at which the search for parent elements should stop. Both the parameters are optional.

        Example

        This example illustrates the working of parentUntil() method.

        HTML

        <body class=“main”> body (great-grandparent)

            <div style=“width:500px;”>div (grandparent)

                <ul>ul (direct parent)

                    <li>li

                    <span>span</span>

                    </li>

                </ul>

            </div>

        </body>

        We have created a div and inside that div we have nested three elements which are

          ,

        • , and .

          jQuery

          $(document).ready(function(){

              $(“li”).parentsUntil(“body”).css({“color”: “blue”, “border”: “2px solid blue”});

          });

          In the above code, we have selected the

        • element and used the parentUntil() method to find all the parents between the
        • , and elements.

          Output

          <img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/image2-47.png" data-lazy- height="335" src="data:image/svg xml,” width=”684″>

          As it can been seen in the output, all the parents of

        • (div, and ul), before have been highlighted.

          closest() Method

          The closest() method fetches the first element that matches the specified element.

          Syntax

          $(selector).closest(filter,context)

          Note: The filter parameter has the same functionality as in other methods, however, it is required in this method. The context parameter, on the other hand, is optional, and specifies a DOM element within which a matching should be found.

          Example

          This example illustrates the working of closest() method.

          <body class=“main”>body (great-great-grandparent)

              <div style=“width:500px;”>div (great/grandparent)

                  <ul>ul (second ancestor/second grandparent)

                      <ul>ul (first ancestor/first grandparent)

                          <li>li (direct parent)

                              <span>span</span>

                          </li>

                      </ul>

                  </ul>

              </div>

          </body>

          We have created a div and inside that div we have nested two

            elements, and one

          • , element.

            jQuery

            $(document).ready(function(){

                $(“span”).closest(“ul”).css({“color”: “blue”, “border”: “2px solid blue”});

            });

            We have applied the closest() method to highlight the first ancestor of the element.

            Output

            <img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/image4-44.png" data-lazy- height="416" src="data:image/svg xml,” width=”684″>

            As it is pointed out in the output, the second

              element is first ancestor of the element.

              Using the above-mentioned methods, you can fetch parent elements of a specified HTML element.

              Conclusion

              To fetch the parent element in jQuery by applying methods such as, parent(), parents(), parentUntil(), and closest(). The parent() method fetches the direct parent of an element, parents() method fetches all the parents of the an element, parentUntil() finds parent elements between two specified elements, and closest() method fetches the first element that matches the specified element. All of these methods, along with their relevant examples are explained in this guide.

  • About the author

    <img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/B49B6FDCE1F74B3598C4C9BF564AEBEC-150×150.jpg621ecde24566b.jpg" height="112" src="data:image/svg xml,” width=”112″>

    Naima Aftab

    I am a software engineering professional with a profound interest in writing. I am pursuing technical writing as my full-time career and sharing my knowledge through my words.