jQuery Tutorial Tutorials - jQuery .filter() method

jQuery .filter() method

The .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

jQuery .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)

example

Filter out elements using selector(:even :gt .class etc)

$('li').filter(':odd').css('background','cyan');

Try now

.filter(function)

example

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');

Try now

.filter(elements)

example

Select all paragraphs then filter the selection with a DOM element, keeping those elements with a class of "selected".

$( "p" ).filter( document.getElementsByClassName( "selected" ) );

Try now

.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" ) );

Try now

Date:2019-08-26 13:31:31 From:www.Lautturi.com author:Lautturi