The 'not(selector) Selector' is used to select the elements, which doesn't match the conditions specified in a selector.
Let us simplify with the below example.
<html>
<head>
<title> My First Programme </title>
</head>
<body>
<h1> JQuery </h1>
<div class = "newClass">
<p class = "para1"> First Paragraph </p>
<p class = "para2"> Second Paragraph </p>
<p class = "para3"> Third Paragraph </p>
</div>
<button> Click me </button>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"> </script>
<script>
$('button').click( function() {
$('p:not(p:first-child)').text("The second and third child of div element got replaced")
});
</script>
</body>
</html>
So, if you see the above code. We can see that there are three <p> elements, those are inside a <div> element.
<div class = "newClass"> <p class = "para1"> First Paragraph </p> <p class = "para2"> Second Paragraph </p> <p class = "para3"> Third Paragraph </p> </div>
Now, if you consider the DOM,
-Selector1.png)
The <div> element has three children of type <p>.
<p class = "para1"> First Paragraph </p> <p class = "para2"> Second Paragraph </p> <p class = "para3"> Third Paragraph </p>
And the first child of <div> element is,
<p class = "para1"> First Paragraph </p>
And on button click, the contents of second and third <p> element gets replaced with 'The second and third child of div element got replaced'.
And this happened with the 'p:not(p:first-child)' element selector.
$('button').click( function() {
$('p:not(p:first-child)').text("The second and third child of div element got replaced")
});The moment the button is clicked, JQuery statement gets triggered.
$('p:not(p:first-child)').text("The second and third child of div element got replaced")So it is a two step process :
<p class = "para1"> First Paragraph </p>
$('p:not(p:first-child)').text("The second and third child of div element got replaced")<p class = "para2"> Second Paragraph </p> <p class = "para3"> Third Paragraph </p>
In simple words, it excludes the first element,
<p class = "para1"> First Paragraph </p>
And displays all other <p> elements.