In jQuery not() method returns elements, which do not match a particular condition. The elements that don’t match the condition are returns from the selection and those match will be removed.

It is used to remove one or more elements from set of selected elements. This not() method is simply opposite to filter() method.

Syntax of jQuery not() method

$(selector).not(criteria,function(index))
For Example
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("p").not(".intro, #outro").css("background-color", "yellow");
});
</script>
</head>
<body>
<h1>Welcome to usejQuery</h1>
<p>Here you can know all about jQuery</p>
<p class="intro">It is an example for jQuery not() function</p>
<p id="outro">How to use jQuery not()</p>
<p>Try it out</p>
</body>
</html>

We can say in other words, not() method filters out all the elements that matches the particular selector from the matched collection of elements.

General Syntax for not()

selector.not(element)
Another example

In the below example, it will filters out the div elements that doesn’t have class=’green’ and changes the border color to red.

<html>
<head>
<title>jQuery traversing not</title>
<style>
  div {
    width: 50px;
    height: 50px;
    margin: 10px;
    float: left;
    background: yellow;
    border: 2px solid white;
  }
 .green {
    background: green;
  }
}
</style>
<script src="https://code.jquery.com/jquery-2.1.1.js"></script>
</head>
<body>
<div></div>
<div class="green"></div>
<div></div>
<script>
  $( "div" ).not( ".green" )
   .css( "border-color", "red" );
</script>
</body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *