Chapter 3. DOM Traversal Methods

Cause there's a train coming into the station

But it's heading for a new destination

—Devo,

"It Doesn't Matter to Me"

In addition to the selector expressions described in Chapter 2, jQuery has a variety of DOM traversal methods to help us select elements in a document. These methods offer a great deal of flexibility, even allowing us to act upon multiple sets of elements in a single chain, like so:

$('div.section > p').addClass('lit').lt(1).addClass('profound');

At times the choice between a selector expression and a corresponding DOM traversal method is simply a matter of taste, but there is no doubt that the combined set of expressions and methods makes for an extremely powerful toolset for getting anything we want.

As of jQuery 1.1, DOM traversal methods do not modify the jQuery object they are sent to. Instead, a new jQuery object is constructed, which contains a reference to the original object. The original object can be retrieved with the .end method.

The following function underpins the entire jQuery library as it allows us to create the jQuery objects that all of the other methods are attached to.