jQuery Selector :parent

Introduction

The :parent selector selects elements which are the parent of another element including text nodes.

$(":parent")

Select all <td> elements with children, including text:

View in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("td:parent").css("background-color", "yellow");
});//  www  .  j a  va  2  s. c o  m
</script>
</head>
<body>

<h1>Welcome to My Web Page</h1>

<table border="1">
  <tr>
    <th>Company</th>
    <th></th>
    <th>Country</th>
  </tr>
  <tr>
    <td>CSS</td>
    <td>Java</td>
    <td></td>
  </tr>
  <tr>
    <td>A</td>
    <td></td>
    <td>Sweden</td>
  </tr>
  <tr>
    <td>Javascript</td>
    <td>Python</td>
    <td>Mexico</td>
  </tr>
  <tr>
    <td>C++</td>
    <td></td>
    <td>XYZ</td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td>UK</td>
  </tr>
  <tr>
    <td>C</td>
    <td>HTML</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>HTML5</td>
    <td>Yes</td>
    <td>Canada</td>
  </tr>
  <tr>
    <td>M</td>
    <td>Name</td>
    <td>Italy</td>
  </tr>
  <tr>
    <td>North/South</td>
    <td>Var</td>
    <td>UK</td>
  </tr>
</table>

<p>Try to write something in a td element that is empty. It will now be selected.</p>

</body>
</html>



PreviousNext

Related