jQuery empty selector

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 ww  w . ja  va2 s. c  om-->
         $("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





















Home »
  jQuery »
    jQuery Tutorial »




Basics
Selector
DOM
Event
Effect
Utilities