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.
$(selector).not(selector-not-to-match)
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.not()
returns all elements that match selector
but do not contain the selector-not-to-match
parameter that is passed.
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.