parent (:parent)

Description and Syntax

$(':parent')

selects all elements that are the parent of another element, including text nodes. The child elements include text nodes.

Examples

  • $(':parent') selects all elements that are the parent of another element, including text nodes
  • $('td:parent') selects all <td> elements that are the parent of another element, including text nodes

The following code selects the table row.


<html>
  <head>
    <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){<!--   www .  ja  v a  2  s  . c  om-->
               
            $("td:parent").fadeTo(1500, 0.3);
        });
    </script>

  </head>
  <body>
    <body>
       <table border="1">
        <tr><td>Value 1</td><td></td></tr>
    
        <tr><td>Value 2</td><td></td></tr>
       </table>

    </body>
</html>

Click to view the demo

The code above generates the following result.

parent (:parent)

Parent of a paragraph

The following code selects the parent of the paragraph.


<!--  w  w  w  . j a va2s  .  c  o  m-->
<html>
  <head>
    <style>
      .test{ border: 1px solid red; }
    </style>
  
    <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
         $("p:parent").addClass("test");
    });
    </script>
  </head>
  <body>
      <div><p>paragraph in div</p></div>
      <div>div</div>
  </body>
</html>

Click to view the demo

The code above generates the following result.

parent (:parent)