The jQuery API .filter() method allows you to filter out certain elements form a parent element that match the specified parameters. You can then do manipulation on these elements that were filtered out. The opposite of the .filter() method is the .not() method.

For example. You have a table of items and you want to filter out all of the items that contain a certain term.

$("table td").filter(":contains('terrier')").css("background-color", "#FFFFBF");

Maybe you wish to filter out and remove all of the elements in an unordered list with a specific class name:

$("ul li").filter(".offtopic").remove();

 

Example of the jQuery API .filter() method

In this example we will filter out all of the elements with class="offtopic" from an unordered list and then use the .remove() method to remove them from the list.. Following is the script for this example:

$("button").click(function(){
    $("ul li").filter(".offtopic").remove();
});

Here is the HTML for this example:

<ul>
    <li>cats</li>
    <li class="offtopic">lampshades</li>
    <li>dogs</li>
    <li>birds</li>
    <li class="offtopic">clouds</li>
</ul>
<button>REMOVE OFFTOPIC OBJECTS</BUTTON>

Here is the result: