I have a div (let\'s say the id is \"container\") with many elements in it, including a select element. I\'d like to select all everything in the div except the select. Thin
:not, and .not() to select and filter » Live DemoThe :not(selector) does exactly this, and it comes in a couple styles. You can use the selector, or the method. Below is an example of using the selector:
$("#container :not(select)");
This will match any child within #container that is not a element. Then we have the method fashion of exclusion, which goes by the same name, but must be ran differently:
$("#container").children().not("select");
This runs against the children of the matched elements. In this case, .not acts as a filter. Which brings me to the next example, using .filter() to get the results you want. With this method, we can provide our own custom function to rummage through the results and pick the ones we want:
.filter() to filter the matched elements » 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 of the children of #container, and then pass them through a filter that we have defined called "isNotSelect". Within our filter, we return either true or false for every element. If we return true, that element will be returned to the result set. If we return false, that element will be removed from the result set. We're asking if the element is not a select. This will return false for all select elements, thus removing them from our collection.