parents() method in jQuery


The parents() method used to select all ancestor elements of the selected element. .

Syntax
//select all parents of <td>
$("td").parent();

//select all <tr> of <td> 
$("td").parent("tr");

Example

The following example shows, how to select all parents of <p> using jQuery parents() method.

Demo
 
<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
    <style>
      div{
        padding:20px;
        border:1px solid #ccc;
      }
    </style>
  </head>
  <body>
    <div>
      <div>
        <p>Hello</p>
      </div>
    </div>
    <script>
    $(document).ready(function(){
      $("p").parents("div").css("border","2px solid red");
    });
    </script>
  </body>
</html>
Try it Yourself