What is the not() method in JQuery?

Overview

The not() method is a JQuery function that is used to return elements that do not match a specified selector. The specified selector can be a class, element, or id selector. not() is the opposite of the filter method, and most times, selectors are used when you have to filter elements.

Syntax

$(selector).not(selector-not-to-match)

Parameters

  • selector: The original element to select and then filter from with the not() method. For example, selector can be p, which selects all paragraph elements.
  • selector-not-to-match: The selector you want to filter away. For example, this parameter can be active and select all paragraph elements that are not active.

Return value

not() returns all elements that match selector but do not contain the selector-not-to-match parameter that is passed.

Example

In the example below, we create some list elements that either have package or framework as a class. We have two buttons that will remove some of these elements based on the filter. When the Remove Framework button is clicked, $("li").not(".package").remove();, the list elements that do not have the package class are removed. When the Remove Package button is clicked, the list items without the framework class are removed.

Free Resources