Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




SELECTORS - :empty Selector


The ':empty Selector' is used to select the elements, that has empty content.


Let us simplify with the below example.


Example :



<html>
  	<head>
    	<title> My First Programme </title>
  	</head>

  	<body>
    	<h1> JQuery </h1>
    	
    	<div class = "newClass1">
    		<p class = "para1"> First Paragraph </p>
    	</div>
    	
    	<div class = "newClass2">
    		<p class = "para2"></p>
    	</div>	
    	
    	<button> Click me </button>

      	<script src = "https://cdnjs.cloudflare.com/ajax/libs/JQUERY/3.3.1/jquery.min.js"> </script>
	  
      	<script>
      
			$('button').click( function() { 
            	$('p:empty').text("This is an empty p element")
            });
            
      	</script>      
  	</body>
</html>


Output :




So, if you see the above code. We can see that there are two <div> elements, and both has a <p> element in it.


<div class = "newClass1">
	<p class = "para1"> First Paragraph </p>
</div>

<div class = "newClass2">
	<p class = "para2"></p>
</div>

And the <p> element of the second <div> has nothing inside it.


<p class = "para2"></p>

And we only want to change the contents of the above <p> element that is empty.


And this happened with the 'p:empty' element selector.


$('button').click( function() {
	$('div:has(p)').text("The content of div that has a p child got changed")
});

The moment the button is clicked, JQuery statement gets triggered.


$('div:has(p)').text("The content of div that has a p child got changed")

And the JQuery code locates the <p> element that is empty and changes its contents.