jQuery | not() method with Examples

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 which 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:
Code #1:
In the below code all the paragraph element except with the class “main” get highlighted with the green color in the background.

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
   
<head>
            jquery/3.3.1/jquery.min.js"></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>

chevron_right


Output:

Code #2:
In the below all the paragraph element with the id “main” get highlighted with the green color in the background.

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
  
<head>
          jquery/3.3.1/jquery.min.js"></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>

chevron_right


Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.