Open In App

jQuery not() method

Improve
Improve
Like Article
Like
Save
Share
Report

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:



Last Updated : 11 Jul, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads