cycling jQuery_Succinctly | Page 48

Chapter 3 Traversing Difference between find() and filter() methods The filter() method is used to filter the current set of elements contained within the wrapper set. Its usage should be left to tasks that require filtering a set of elements that are already selected. For example, the code below will filter the three

elements contained in the wrapper set. Sample: sample36.html

first

middle

last

Notes: When using filter(), always ask yourself if it is absolutely necessary. For example, $('p').filter(':not(:first):not(:last)') could be written without filter() by passing the jQuery function the expressions as custom selectors $('p:not(:first):not(:last)'). The find() method, on the other hand, can be used to further find descendants of the currently selected elements. Think of find() more like updating or changing the current wrapped set with new elements that are encapsulated within the elements that are already selected. For example, the code below will change the wrapped set from

elements to two elements by using find(). Sample: sample37.html

first

48