.filter() | jQuery filter text
Description:Reducethesetofmatchedelementstothosethatmatchtheselectororpassthefunctionstest.GivenajQueryobjectthatrepresentsasetofDOMelements,the.filter()methodconstructsanewjQueryobjectfromasubsetofthematchingelements.Thesuppliedselectoristestedagainsteachelement;allelementsmatchingtheselectorwillbeincludedintheresult.Considerapagewithasimplelistonit:Wecanapplythismethodtothesetoflistitems:1$("li").filter(":nth-child(2n)").css("background-color","red");Theresultofthiscallisaredbackgroundfori...
Description: Reduce the set of matched elements to those that match the selector or pass the functions test.
Given a jQuery object that represents a set of DOM elements, the .filter() method constructs a new jQuery object from a subset of the matching elements. The supplied selector is tested against each element; all elements matching the selector will be included in the result.
Consider a page with a simple list on it:
We can apply this method to the set of list items:
1
$( "li" ).filter( ":nth-child(2n)" ).css( "background-color", "red" );
The result of this call is a red background for items 2, 4, and 6, as they match the selector.
Using a Filter FunctionThe second form of this method allows us to filter elements against a function rather than a selector. For each element, if the function returns true (or a "truthy" value), the element will be included ...