jQuery – Empty selector example

In jQuery, the “empty” selector is used to select all elements that have no children (including any text inside).

Examples


<div class="div-class1">
   This is div-class1
</div>

<div class="div-class2" />

$(‘:empty’) – The “div-class2” is matched, while the “div-class1” is not.

jQuery Example

A simple example to show the use of the jQuery “empty” selector.


<html>
<head>
<title>jQuery empty example</title>
 
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>

<style type="text/css">
	div{
		padding:8px;
		border:1px solid;
	}
</style>
</head>

<body>

<h1>jQuery empty example</h1>

<div class="div-class1">
	This is div-class1
</div>

<div class="div-class2" />

<div class="div-class3">
	This is div-class3
	<div class="div-class3-1">
		This is div-class3-1
	</div>	
</div>

<br/><br/>

<button>:empty</button>

<script type="text/javascript">
    $("button").click(function () {
      var str = $(this).text();
      $("*").css("background", "white");
      $(str).css("background", "coral");
    });
</script>

</body>
</html>

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments