Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between find and filter in jquery

I'm working on fetching data from wiki pages. I'm using a combination of php and jquery to do this. First I am using curl in php to fetch page contents and echoing the content. The filename is content.php:

$url = $_GET['url']; $url = trim($url," "); $url = urldecode($url); $url = str_replace(" ","%20",$url);  echo "<a class='urlmax'>".$_GET['title']."</a>"; echo crawl($url); 

Then jQuery is used to find the matched elements.

$.get("content.php",{url:"http://en.wikipedia.org/w/index.php?action=render&title="+str_replace(" ","_",data[x]),title:str_replace(" ","_",data[x])},function(hdata){                         var imgs = $(hdata).find('a.image img');                         var ent = $(hdata).filter('a.urlmax');                           ent = $(ent[0]).text();   }); 

I was able to successfully get images but for the variable ent when I use find instead of filter, it's returning an empty array. Only filter is working. Why is this?

Edit: I know the basic difference between find and filter. Here both the a.image img and a.urlmax are descendats of the hdata. Then why find does not work on a.urlmax. Not a.urlmax alone it's not working on any other class or id

like image 994
Krishna Deepak Avatar asked Apr 30 '12 05:04

Krishna Deepak


People also ask

What is find () in jQuery?

The find() is an inbuilt method in jQuery which is used to find all the descendant elements of the selected element. It will traverse all the way down to the last leaf of the selected element in the DOM tree. Syntax: $(selector).find()

What is the use of filter in jQuery?

The filter() method returns elements that match a certain criteria. This method lets you specify a criteria. Elements that do not match the criteria are removed from the selection, and those that match will be returned. This method is often used to narrow down the search for an element in a group of selected elements.

How do you use Find and filter in Javascript?

Find and FilterThe find() method returns the first value that matches from the collection. Once it matches the value in findings, it will not check the remaining values in the array collection. The filter() method returns the matched values in an array from the collection.

What is the difference between filter and map?

map creates a new array by transforming every element in an array individually. filter creates a new array by removing elements that don't belong. reduce , on the other hand, takes all of the elements in an array and reduces them into a single value. Just like map and filter , reduce is defined on Array.


2 Answers

.find() http://api.jquery.com/find/

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

Filter, on the other hand, works on the currently matched elements. That's why filter worked but find did not (you needed to look at the current element).

like image 194
Jere Avatar answered Sep 28 '22 18:09

Jere


filter will select subset of element from the selected element

find will select descendent/children of selected element

To make it more clear filter will search through all element whereas find will search only in the descendent list

like image 23
mprabhat Avatar answered Sep 28 '22 16:09

mprabhat