Sunday 24 September 2017

jQuery Filter vs Find

Filter and Find both are nearly similar but there is one difference.


Filter: Looks though the elements at all levels(based on the selector we give), whether it's a child of    some other element or it is itself a parent.

Find: This looks only at the child level.

Example: This will help you understand the difference. And you will realize, how important both are in their own way.

1. Find: This will only select the <div id="Fruits"> inside the <div id="Category">.
$('div').find('#Fruits')
2. Filter: Whereas this will select 2 divs => a) <div id="Fruits"> which is first parent div. b) The <div id="Fruits"> inside the Category div.
    $('div').filter('#Fruits')

HTML :-
<html><body>

<div id="Fruits">
 Fruits
 <div id="Apple">Apple</div>
 <div id="Banana">Banana</div>
</div>

<div id="Category">
 Category
 <div id="Fruits">Fruits</div>
 <div id="Animals">Animals</div>
</div> </body></html>

No comments:

Post a Comment