cycling jQuery_Succinctly | Page 49

middle

last

Notes: You can actually combine the elements in the wrapper previous to using the find() method with the current elements by using andSelf()—e.g. $('p').find('strong').andSelf(). The concept to take away is that filter() will only reduce (or filter) the currently selected elements in the wrapper set while find() can actually create an entirely new set of wrapped elements. Notes: Both find() and filter()are destructive methods that can be undone by using end(), which will revert the wrapped set back to its previous state before find() or filter() were used. Passing filter() a function instead of an expression Before you run off and create a custom filter for selecting elements, it might make more sense to simply pass the traversing filter() method a function that will allow you to examine each element in the wrapper set for a particular scenario. For example, let's say you would like to wrap all elements in an HTML page with a

element that is currently not wrapped with this element. You could create a custom filter to accomplish this task, or you could use the filter() method by passing it a function that will determine if the element's parent is a

element, and if not, then remove the element from the set before you wrap the elements remaining in the set with a

element. In the following example, I select every element in the HTML page, and then I pass the filter() method a function that is used to iterate over each element (using this) in the wrapper set, checking to see if the elements’ parent element is a

element. 49