jQuery – :empty Selector

The jQuery empty selector select all elements that have no children (including text nodes). :empty inverse of :parent.

Source Code

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>

<table>
  <tr><td>FName</td><td></td></tr>
  <tr><td>LName</td><td></td></tr>

  <tr><td></td><td>Delhi</td></tr>
</table>
<button>Click Here</button>

  
<script>
jQuery("button").click(function () {
	jQuery("td:empty").text("Empty!!!").css({background:"#1E8CBE",color:"white"});
});
</script>
</body>
</html>

Demo

Following example finds all elements that are empty i.e. they don’t have child elements or text and replace them with some text.

FName
LName
Delhi

,

No comments yet.

Leave a Reply