tadam logo
Did you find an error in the text?
Select this with mouse and press
Ctrl + Enter
Xhtml.co.il Check Spelling
Orphus system

.not( selector )

Returns: jQuery

Description: Remove elements from the set of matched elements.

.not( selector )

version added: 1.0
selector
A string containing a selector expression to match elements against.

.not( elements )

version added: 1.0
elements
One or more DOM elements to remove from the matched set.

.not( function(index) )

version added: 1.4
function(index)
A function used as a test for each element in the set. this is the current DOM element.

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:

<ul>
  <li>list item 1</li>
  <li>list item 2</li>
  <li>list item 3</li>
  <li>list item 4</li>
  <li>list item 5</li>
</ul>
We can apply this method to the set of list items:
$('li').not(':even').css('background-color', 'red');
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:

<ul>
  <li>list item 1</li>
  <li>list item 2</li>
  <li id="notli">list item 3</li>
  <li>list item 4</li>
  <li>list item 5</li>
</ul>
We can fetch the third list item using the native JavaScript getElementById() function, then remove it from a jQuery object:
$('li').not(document.getElementById('notli'))
  .css('background-color', 'red');

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 1

Adds a border to divs that are not green or blue.
    $("div").not(".green, #blueone")
            .css("border-color", "red");

The output of the code above will be:

Example 1 - Full source:

Adds a border to divs that are not green or blue.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<script src="http://code.jquery.com/jquery-latest.js"></script>
  
  <script type="text/javascript" language="javascript">

  $(document).ready(function(){
    
    $("div").not(".green, #blueone")
            .css("border-color", "red");


  });
  </script>

  <style>
  div { width:60px; height:60px; margin:10px; float:left;
        background:yellow; border:2px solid white; }
  .green { background:#8f8; }
  .gray { background:#ccc; }
  #blueone { background:#99f; }
  </style>
</head>

<body>
  <div></div>
  <div id="blueone"></div>

  <div></div>
  <div class="green"></div>

  <div class="green"></div>
  <div class="gray"></div>

  <div></div>
</body>
</html>

Example 2

Removes the element with the ID "selected" from the set of all paragraphs.
$("p").not( $("#selected")[0] )

Example 3

Removes the element with the ID "selected" from the set of all paragraphs.
$("p").not("#selected")

Example 4

Removes all elements that match "div p.selected" from the total set of all paragraphs.
$("p").not($("div p.selected"))
Was this information helpful?
   

Comments