.filter()
methodThe .filter(N)
method in jQuery is used to filter out the elements from the set of matched elements. Each filtered element should match the selector or pass the function's test
.filter()
method Syntax.filter(selector) .filter(function) .filter(elements) .filter(selection)
Parameter | Type | Description |
---|---|---|
selector |
selector | a selector expression. |
function() |
Function | A function used to detemine whether each element in the set should be filtered out. |
elements |
Element | DOM elements |
selection |
jQuery | A jQuery object |
.filter(selector)
Filter out elements using selector(:even :gt .class etc)
$('li').filter(':odd').css('background','cyan');
.filter(function)
Filter elements against a function
Highlight the items which's content contains "tutorial" substring.
$('li').filter(function(index,element){ return ($(this).text().indexOf('tutorial')>=0)?true:false; }).css('background','cyan');
.filter(elements)
Select all paragraphs then filter the selection with a DOM element, keeping those elements with a class of "selected".
$( "p" ).filter( document.getElementsByClassName( "selected" ) );
.filter(selection)
Select all divs and filter the selection with a jQuery object, keeping only the one with an id of "unique".
$( "div" ).filter( $( "#unique" ) );