(^60) ❘ CHAPTER 2 SELECTING AND FILTERING
$('ul#middleEarthPlaces li').add(
document.getElementById('middleEarthMorePlaces').childNodes
);
And you can use callback functions that return either jQuery selections or direct DOM object
references:
$('ul#middleEarthPlaces li').add(
function()
{
return document.getElementById('middleEarthMorePlaces').childNodes;
}
);
The add() method allows you to add elements to a selection using any of these methods. In Chapter 4,
“Manipulating Content and Attributes,” you’ll learn how you can even use a string containing HTML
to add to a selection.
NOTE Appendix C, “Selecting, Traversing, and Filtering,” provides a reference
for all jQuery’s selection and fi ltering methods.
Summary
In this chapter, you’ve seen some examples that give you a comprehensive overview of jQuery’s selec-
tion and fi ltering abilities. You learned how jQuery provides ridiculously fi ne-grained control over
selecting elements from the DOM, so fi ne-grained that you’ll often fi nd that there are multiple ways
to achieve the same results.
jQuery’s selection and fi ltering methods go much further than what you get with JavaScript alone,
which more often than not would take several lines of code to come to the same level of control over
a selection.
jQuery harnesses the power, ease, familiarity, and convenience of selectors to help you get any-
where in the DOM you want to go. The selector syntax, you’ll fi nd, is the same as what you’re used
to using with CSS; jQuery even supports a few extensions of its own. See Appendix B, “jQuery
Selectors,” for a full listing of selector syntax supported by jQuery.
jQuery’s fi ltering methods let you select descendants using the find() method, ancestors using the
parents() method, and siblings using the siblings(), prev(), prevAll(), next(), and nextAll()
methods. You can add elements using the add() method or exclude elements using the not() method.
And you can also get even more specifi c using the slice() and eq() methods. See Appendix C for a
full list of methods related to selection and fi ltering.