:empty Selector in JQuery



version added: 1.0jQuery(‘:empty’)

This is the inverse of :parent.

One important thing to note with :empty (and :parent) is that child elements include text nodes.

The W3C recommends that the <p> element have at least one child node, even if that child is merely text (see http://www.w3.org/TR/html401/struct/text.html#edef-P). Some other elements, on the other hand, are empty (i.e. have no children) by definition: <input>, <img>, <br>, and <hr>, for example.


Finds all elements that are empty – they don’t have child elements or text.

<!DOCTYPE html>

  td { text-align:center; }
  <script src="/scripts/jquery-1.4.js"></script>
	<table border="1">
    <tr><td>TD #0</td><td></td></tr>
    <tr><td>TD #2</td><td></td></tr>

<script>$("td:empty").text("Was empty!").css('background', 'rgb(255,220,200)');</script>

Leave a Reply

Your email address will not be published. Required fields are marked *