Filtering Selections and Arrays (^) ❘ 145
The following script demonstrates how the filter() method uses a selector to indicate which items
should be in the selection:
$(document).ready(
function()
{
$('ul#rubberSoul li')
.filter('li.George')
.addClass('rubberSoulGeorge')
.end()
.filter('li.John')
.addClass('rubberSoulJohn')
.end()
.filter('li.Paul')
.addClass('rubberSoulPaul')
.end();
}
);
In the preceding script, the selector li.George reduces the selection to include only the
elements that have a class name of George; then the class name rubberSoulGeorge is added to each of
those
is attempted, a call to end() removes the last fi lter applied to the selection. Figure 5-4 shows this
example in Safari.