Empty (:empty)

Description and Syntax

$(':empty')

selects all elements that have no children (including text nodes). The child elements include text nodes.

Examples

SelectorSelects
$(':empty')all elements in the document that have no children
$('p:empty')all <p> elements that have no children

The following code selects the empty table cells.


<html>
  <head>
    <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){<!--from  w w  w  .  j a  v  a 2s  .  co  m-->
         $("td:empty").text("Was empty").css('background', 'rgb(255,220,200)');
    });
    </script>
  </head>
  <body>
      <table>
          <tr><td>data</td><td></td></tr>
          <tr><td>data</td><td></td></tr>
          <tr><td>data</td><td></td></tr>
      </table>
  </body>
</html>

Click to view the demo

The code above generates the following result.

Empty (:empty)