The not() is an inbuilt function in jQuery which is just opposite to the filter() method. This function will return all the element which is not matched with the selected element with the particular “id” or “class”.
Syntax:
$(selector).not(A)
The selector is the selected element that is not to be selected.
Parameters: It accepts a parameter “A” which is either “id” or “class” of the specified element.
Return Value: This will return all the elements except the selected elements.
JavaScript code to show the working of this function:
Example 1: In the below code all the paragraph elements except the class “main” get highlighted with the green color in the background.
html
<!DOCTYPE html>
< html >
< head >
< script src =
</ script >
< script >
$(document).ready(function () {
$("p").not(".main").css("background-color", "green");
});
</ script >
</ head >
< body >
< p class = "main" >Hello !!!</ p >
< p class = "main" >Welcome to</ p >
< p >GeeksforGeeks.!!!</ p >
</ body >
</ html >
|
Output:

Example 2: In the below all the paragraph elements with the id “main” get highlighted with the green color in the background.
html
<!DOCTYPE html>
< html >
< head >
< script src =
</ script >
< script >
$(document).ready(function () {
$("p").filter("#main").css("background-color", "green");
});
</ script >
</ head >
< body >
< p id = "main" >GeeksforGeeks.!!!</ p >
< p id = "main" >Hello !!!</ p >
< p >This is not() method of jQuery.!!!</ p >
</ body >
</ html >
|
Output:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!