![jQuery Tutorial](/jquery/images/jquery-mini-logo.jpg)
- jQuery Tutorial
- jQuery - Home
- jQuery - Overview
- jQuery - Basics
- jQuery - Syntax
- jQuery - Selectors
- jQuery - Events
- jQuery - Attributes
- jQuery - AJAX
- jQuery DOM Manipulation
- jQuery - DOM
- jQuery - Add Elements
- jQuery - Remove Elements
- jQuery - Replace Elements
- jQuery CSS Manipulation
- jQuery - CSS Classes
- jQuery - Dimensions
- jQuery - CSS Properties
- jQuery Traversing
- jQuery - Traversing
- jQuery - Traversing Ancestors
- jQuery - Traversing Descendants
- jQuery References
- jQuery - Selectors
- jQuery - Events
- jQuery - Effects
- jQuery - HTML/CSS
- jQuery - Traversing
- jQuery - Miscellaneous
- jQuery - Properties
- jQuery - Utilities
- jQuery Plugins
- jQuery - Plugins
- jQuery - PagePiling.js
- jQuery - Flickerplate.js
- jQuery - Multiscroll.js
- jQuery - Slidebar.js
- jQuery - Rowgrid.js
- jQuery - Alertify.js
- jQuery - Progressbar.js
- jQuery - Slideshow.js
- jQuery - Drawsvg.js
- jQuery - Tagsort.js
- jQuery - LogosDistort.js
- jQuery - Filer.js
- jQuery - Whatsnearby.js
- jQuery - Checkout.js
- jQuery - Blockrain.js
- jQuery - Producttour.js
- jQuery - Megadropdown.js
- jQuery - Weather.js
- jQuery Useful Resources
- jQuery - Questions and Answers
- jQuery - Quick Guide
- jQuery - Useful Resources
- jQuery - Discussion
jQuery not() Method
The not() method in jQuery is used to filter out elements from the set of matched elements. In other words, It removes elements that match the specified selector from the set of matched elements.
Syntax
Following is the syntax of not() method in jQuery −
$(selector).not(criteria,function(index))
Parameters
This method accepts the following parameters −
- criteria: A selector expression, element, or jQuery object indicating the elements to be removed.
- function(index): Optional callback function to execute for each element in the matching set, where index represents the index of the current element.
Example 1
In the following example, we are using the not() method to select all elements that do not have the class name "demo" −
<html> <head> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").not(".demo").css("border", "2px solid green") }) }); </script> </head> <body> <p class="demo">Paragraph element (with class demo).</p> <p>Paragraph element.</p> <p class="demo">Paragraph element (with class demo).</p> <p>Paragraph element.</p> <p class="demo">Paragraph element (with class demo).</p> <button>Click</button> </body> </html>
When we execute the above program, all the elements without class name "demo" will be highlighted with solid green border.
Example 2
In this example, we are returning all elements that do not have the class "demo" and id "dummy" −
<html> <head> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").not(".demo, .dummy").css("border", "2px solid green") }) }); </script> </head> <body> <p class="demo">Paragraph element (with class demo).</p> <p>Paragraph element.</p> <p class="demo">Paragraph element (with class demo).</p> <p>Paragraph element.</p> <p>Paragraph element.</p> <p class="dummy">Paragraph element (with class dummy).</p> <button>Click</button> </body> </html>
When we execute the above program, all the elements without class names "demo" and "dummy" will be highlighted with solid green border.
Example 3
Here, we are selecting all the <p> elements that do not have the class "demo" and id "dummy" −
<html> <head> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").not($("p.demo, p.dummy")).css("border", "2px solid green") }) }); </script> </head> <body> <p class="demo">Paragraph element (with class demo).</p> <p>Paragraph element.</p> <p class="demo">Paragraph element (with class demo).</p> <p>Paragraph element.</p> <p>Paragraph element.</p> <p class="dummy">Paragraph element (with class dummy).</p> <button>Click</button> </body> </html>
After executing the above program, all the <p> elements without class names "demo" and "dummy" will be highlighted with solid green border.
Example 4
Here, we are returning all elements that do not have the id "demo", with a DOM element −
<html> <head> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").not(document.getElementById("demo")).css("border", "2px solid green") }) }); </script> </head> <body> <p id="demo">Paragraph element (with class demo).</p> <p>Paragraph element.</p> <p>Paragraph element.</p> <p>Paragraph element.</p> <button>Click</button> </body> </html>
After executing the above program, all the elements without class name "demo" will be highlighted with solid green border.