user3108114
user3108114

Reputation: 1

What is the difference between find and filter in Jquery?

$('#navs li').find('.activenav').filter('.activenav').removeClass('activenav');

or

$('#navs li').filter('.activenav').find('.activenav').removeClass('activenav');

Upvotes: 0

Views: 125

Answers (4)

Owen
Owen

Reputation: 151

.filter will find entries in the returned array that also match the filter query, and .find queries each of those to find children that match the query you entered.

With the markup:

<div class="cheese">
    <div class="cheddar"></div>
    <div class="wensleydale"></div>
    <div class="brie">
                <div class="camembert"></div>
    </div>
</div>

This:

$('.cheese div').filter('.brie');

Would return the brie div, but this:

$('.cheese div').filter('.camembert');

Would return nothing, when this:

$('.cheese div').find('.camembert');

Would search deeper, and retrieve the camembert div.

Which is a shame, because Camembert is disgusting.

Upvotes: 2

Samuel
Samuel

Reputation: 2156

find will search in the decedents for matching elements

filter will keep only the matching elements from a set of elements.

Upvotes: 0

Charlie74
Charlie74

Reputation: 2903

According to this link... http://www.mkyong.com/jquery/difference-between-filter-and-find-in-jquery/

Both filter() and find() methods are very similar, except the former is applies to all the elements, while latter searches child elements only.

Upvotes: 0

Code Maverick
Code Maverick

Reputation: 20415

.find()

Description: Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element.


.filter()

Description: Reduce the set of matched elements to those that match the selector or pass the function's test.

Upvotes: 3

Related Questions