Read more

Traversing the DOM tree with jQuery

Henning Koch
March 11, 2011Software engineer at makandra GmbH

jQuery offers many different methods Show archive.org snapshot to move a selection through the DOM tree. These are the most important:

Illustration online protection

Rails professionals since 2007

Our laser focus on a single technology has made us a leader in this space. Need help?

  • We build a solid first version of your product
  • We train your development team
  • We rescue your project in trouble
Read more Show archive.org snapshot

$element.find(selector) Show archive.org snapshot
Get the descendants of each element in the current set of matched elements, filtered by a selector. Does not find the current element, even it matches. If you wanted to do that, you need to write $element.find(selector).addBack(selector).

$element.closest(selector) Show archive.org snapshot
Get the first ancestor element that matches the selector. This also finds the current element, if it matches the selector.

$element.children(selector) Show archive.org snapshot
Get the immediate children of each element in the set of matched elements, optionally filtered by a selector.

$element.parents(selector) Show archive.org snapshot
Get the ancestors of each element in the current set of matched elements, optionally filtered by a selector. Despite the name, this looks at all ancestors, not just the immediate parents.

$element.parent(selector) Show archive.org snapshot
Get the immediate parent of each element in the current set of matched elements, optionally filtered by a selector.

$element.filter(selector_or_function) Show archive.org snapshot
Reduce the set of matched elements to those that match the selector or pass the function's test.

jQuery has a select method, but it has nothing to do with navigating the DOM tree. You are probably thinking about Prototype's select method Show archive.org snapshot .

If you would like to transport your jQuery knowledge to the native DOM API, see DOM API for jQuery users.

Posted by Henning Koch to makandra dev (2011-03-11 10:49)