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




SELECTORS - attribute~=value Selector


The 'attribute~=value Selector' is used to select the elements of an attribute, that contains a particular value.


Say, for example, <p>, <div>, <h1> e.t.c. are called elements in HTML.


And the <p> or <div> element can have a 'class' or 'id'.


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

These 'class' or 'id' are called as attribute in HTML.


Let us learn more with the below example.


Example :



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

  	<body>
    	<h1> JQuery </h1>
    
    	<p class = "Para"> First Paragraph </p>
    	<p class = "Paragraph"> Next Paragraph </p>
   		<p class = "Para-next"> New Next Paragraph </p>
    	
    	<button> Click me </button>

      	<script src = "https://cdnjs.cloudflare.com/ajax/libs/JQUERY/3.3.1/jquery.min.js"> </script>
	  
      	<script>
      
			$('button').click( function() { 
            	$('[class ~= "Para"]').text("The contents of the elements got changed")
            });
            
      	</script>      
  	</body>
</html>


Output :




So, if you see the above code. We can see that there are three <p> elements,


<p class = "Para"> First Paragraph </p>
<p class = "Paragraph"> Next Paragraph </p>
<p class = "Para-next"> New Next Paragraph </p>

And we only want to change the contents of those elements that has a class name that contains 'Para'.


And as we can see, the first <p> element,


<p class = "Para"> First Paragraph </p>

Has the name 'Para'.


Thus the contents of,


<p class = "Para"> First Paragraph </p>

Gets changed.


And this happened with the '[class ~= "Para"]' element selector.


$('button').click( function() {
	$('[class ~= "Para"]').text("The contents of the elements got changed")
});

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


$('[class ~= "Para"]').text("The contents of the elements got changed")

And the JQuery code locates those elements that has a class name that contains 'Para'.


And changes the contents of,


<p class = "Para"> First Paragraph </p>