Related Articles

Related Articles

jQuery | not() method with Examples
  • Last Updated : 13 Feb, 2019

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:

full-stack-img




My Personal Notes arrow_drop_up
Recommended Articles
Page :