The 'attribute!=value Selector' is used to select the elements, that doesn't match a particular attribute with a specific 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.
<html> <head> <title> My First Programme </title> </head> <body> <h1> JQuery </h1> <p class = "para1"> First Paragraph </p> <p class = "newPara"> Next Paragraph </p> <p id = "nextPara"> 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() { $('p[class != "newPara"]').text("The contents of the elements with attribute value that is not class=newPara got changed") }); </script> </body> </html>
So, if you see the above code. We can see that there are three <p> elements, where the first two <p> elements has a 'class' attribute.
<p class = "para1"> First Paragraph </p> <p class = "newPara"> Next Paragraph </p>
And the third <p> element has an 'id' attribute in it.
<p id = "nextPara"> New Next Paragraph </p>
And we only want to change the contents of those <p> elements that doesn't have 'class = "newPara"' attribute in it.
And as we can see,
<p class = "para1"> First Paragraph </p>
And
<p id = "nextPara"> New Next Paragraph </p>
Doesn't have 'class = "newPara"'. So, JQuery would be changing the contents of them.
And this happened with the '[class != "newPara"]' element selector.
$('button').click( function() { $('p[class != "newPara"]').text("The contents of the elements with attribute value that is not class=newPara got changed") });
The moment the button is clicked, JQuery statement gets triggered.
$('p[class != "newPara"]').text("The contents of the elements with attribute value that is not class=newPara got changed")
And the JQuery code locates the elements with class attribute 'class = "newPara"' and keeps it as is.
And changes the contents of,
<p class = "para1"> First Paragraph </p>
And
<p id = "nextPara"> New Next Paragraph </p>