Using :not and .not() to select and filter & raquo; Live demo
:not(selector) does just that, and it comes in several styles. You can use a selector or method. The following is an example of using a selector:
$("#container :not(select)");
This will match any child #container that is not a <select> element. Then we have an exception method that has the same name, but must be executed differently:
$("#container").children().not("select");
This runs against children matched elements. In this case .not acts as a filter. This leads me to the following example, using .filter() to get the desired results. With this method, we can provide our own custom function to rummage through the results and select the ones we want:
Using .filter() to filter matched elements & raquo; Live demo
$( "#container" ).children().filter( isNotSELECT ).fadeTo( "slow", .5 ); function isNotSELECT () { // Returns TRUE if element is NOT select return !$(this).is("select"); }
In this example, we select all the #container and then pass them through the filter we defined, called "isNotSelect". In our filter, we return either true or false for each element. If we return true , this element will be returned to the result set. If we return false , this item will be removed from the result set. We ask if the item is selected. This will return false for all select elements, removing them from our collection.
Sampson Jul 14 '09 at 14:34 2009-07-14 14:34
source share