.not( selector ) Returns: jQuery
Description: Remove elements from the set of matched elements.
-
version added: 1.0.not( selector )
-
selectorType: SelectorA string containing a selector expression to match elements against.
-
-
version added: 1.0.not( elements )
-
elementsType: ElementsOne or more DOM elements to remove from the matched set.
-
-
version added: 1.4.not( function(index) )
-
function(index)Type: Function()A function used as a test for each element in the set.
this
is the current DOM element.
-
-
version added: 1.4.not( jQuery object )
-
jQuery objectType: PlainObjectAn existing jQuery object to match the current set of elements against.
-
Given a jQuery object that represents a set of DOM elements, the .not()
method constructs a new jQuery object from a subset of the matching elements. The supplied selector is tested against each element; the elements that don't match the selector will be included in the result.
Consider a page with a simple list on it:
1
2
3
4
5
6
7
|
|
We can apply this method to the set of list items:
1
|
|
The result of this call is a red background for items 2 and 4, as they do not match the selector (recall that :even and :odd use 0-based indexing).
Removing Specific Elements
The second version of the .not()
method allows us to remove elements from the matched set, assuming we have found those elements previously by some other means. For example, suppose our list had an id applied to one of its items:
1
2
3
4
5
6
7
|
|
We can fetch the third list item using the native JavaScript getElementById()
function, then remove it from a jQuery object:
1
2
|
|
This statement changes the color of items 1, 2, 4, and 5. We could have accomplished the same thing with a simpler jQuery expression, but this technique can be useful when, for example, other libraries provide references to plain DOM nodes.
As of jQuery 1.4, the .not()
method can take a function as its argument in the same way that .filter()
does. Elements for which the function returns true
are excluded from the filtered set; all other elements are included.
Examples:
Example: Adds a border to divs that are not green or blue.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
|
Example: Removes the element with the ID "selected" from the set of all paragraphs.
1
|
|
Example: Removes the element with the ID "selected" from the set of all paragraphs.
1
|
|
Example: Removes all elements that match "div p.selected" from the total set of all paragraphs.
1
|
|