.parents()

In this chapter you will learn:

  1. Syntax and Description for .parents()
  2. Count the parent elements by tag name
  3. How to map parent elements to get all their names
  4. Find all unique div parent elements of each span

Syntax and Description

.parents([selector])

gets the ancestors of each element in the current set of matched elements, optionally filtered by a selector. selector (optional) is a string containing a selector expression to match elements against. Its return value is the new jQuery object.

Count the parent elements by tag name

The following code gets DIV parent count

<html><!--from  j  av  a2 s . c o m-->
  <head>
    <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
             $("span").click(function () {
                 var len = $("span").parents("div").length;
                 alert(len);
             });
        });
    </script>
  </head>
  <body>
    <body>
        <div>
          <div><span>Hello ja va2s.com</span></div>
        </div>
  </body>
</html>

Click to view the demo

Map the parents

The following code gets parents and then uses the map function to get all of their tag names.

<html><!--from ja  v  a 2 s.c o  m-->
  <head>
    <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
             var parentEls = $("span").parents()
                          .map(function () {
                                return this.tagName;
                              })
                          .get().join(", ");
             $("span").append(parentEls);
        });
    </script>
  </head>
  <body>
    <body>
       <div>
         <p>
          <span>
          </span>
         </p>
       </div>

  </body>
</html>

Click to view the demo

Find all unique div parent elements of each span

Click to find all unique div parent elements of each span.

<html><!--from   j  a  va 2s .c  o  m-->
  <head>
    <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
    function showParents() {

              var len = $("span")
                               .parents("div")
                               .css("border", "2px red solid")
                               .length;
              $("b").text("Unique div parents: " + len);
            }
            $("span").click(function () {

              showParents();
            });
        });
    </script>
    <style>
      .selected { color:blue; }

    </style>

  </head>
  <body>
    <body>
        <div>
          <div><span>span</span></div>
          <span>span</span>
        </div>
        <div>
          <span>java2s.com</span>
        </div>
        <b></b>
    </body>
</html>

Click to view the demo

Next chapter...

What you will learn in the next chapter:

  1. Syntax and Description for .parentsUntil()