jQuery parent selector

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(){<!--from   ww  w  .  j  av  a 2s  . co m-->
               
            $("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

Parent of a paragraph

The following code selects the parent of the paragraph.


<!-- w  w w  . j  av a2 s.  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





















Home »
  jQuery »
    jQuery Tutorial »




Basics
Selector
DOM
Event
Effect
Utilities