Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 56 ยท Traversing the DOM

Traversing Down and Sideways

Walk down to children and descendants and sideways to siblings with jQuery .children(), .find(), .contents(), .siblings(), .next(), .nextAll(), .prev() and more.

Going up answers "what contains me?". The other two directions answer "what is inside me?" and "who sits next to me?". With down methods you reach the items of a list from the list itself, and with sideways methods you move between neighbours: the label after an input, the next step in a wizard, the row above the current one.

Every method here starts from the elements in your current set and returns a new jQuery object. Most accept an optional selector that keeps only matching results, which gives you the same filtering power as in the filtering methods.

Down the tree

children Direct child elements only.
find Any descendant that matches.
contents Children including text and comments.
1 / 2

Going down

We will use this list in the examples:

HTML
<ul id="menu">
  <li class="a">Home</li>
  <li class="b">Shop
    <ul><li class="b1">Lamps</li><li class="b2">Chairs</li></ul>
  </li>
  <li class="c">Blog</li>
  <li class="d">About</li>
</ul>

.children(): one level down

.children() returns the direct child elements. Text nodes are ignored.

JavaScript
$("#menu").children();          // the four top-level li
$("#menu").children(".c");      // li.c

It never goes deeper. The two nested li elements inside "Shop" are not children of #menu.

.find(): any depth

.find(selector) searches all descendants. Unlike the other methods in this lesson, it requires a selector (or an element or jQuery object); there is no argument-free form.

JavaScript
$("#menu").find("li");          // all six li, nested ones included
$("#menu").find("ul li");       // just the nested two
$("#menu").find("*");           // every descendant element

Use .children() when you mean the direct level and .find() when you do not care how deep the match is. The difference is the usual cause of "my selector found too many items" bugs with nested lists.

A related detail: .find("li") searches only inside #menu. That is why $("#menu").find("li") is faster and safer than $("li"), as discussed in selector performance.

.contents(): including text

.contents() returns all children as nodes, so text nodes and comments are included. It is the tool for processing mixed content such as <li>Shop <ul>...</ul></li>, where the label "Shop" is a text node that .children() cannot see.

JavaScript
const label = $(".b").contents().first().text().trim();   // "Shop"
$(".b").contents().filter(function () {
  return this.nodeType === 3;      // keep only text nodes
});

It also returns the document of an iframe when called on an <iframe> from the same origin.

Going sideways

Siblings are elements that share the same parent.

.siblings(): everyone else

.siblings() returns all siblings of each element except the element itself.

JavaScript
$(".b").siblings();           // li.a, li.c, li.d
$(".b").siblings(".d");       // li.d

This method is perfect for "select one, deselect the others" behaviour:

JavaScript
$(this).addClass("active").siblings().removeClass("active");

.next() and .prev()

.next() returns the immediately following sibling, .prev() the immediately preceding one. With a selector, you get the sibling only if it matches.

JavaScript
$(".b").next();         // li.c
$(".b").next(".d");     // empty: the next sibling is li.c, not li.d
$(".b").prev();         // li.a
$(".a").prev();         // empty set: nothing comes before it

Notice that an empty result is safe to chain: calling methods on an empty jQuery object does nothing and does not throw.

.nextAll() and .prevAll()

These return all following (or preceding) siblings, ordered from the closest outward.

JavaScript
$(".a").nextAll();       // li.b, li.c, li.d
$(".d").prevAll();       // li.c, li.b, li.a (nearest first)

.nextUntil() and .prevUntil()

These return the siblings after (or before) the element up to, but not including, the first one matching the selector. They are ideal for flat structures where a heading is followed by paragraphs and the section ends at the next heading.

JavaScript
$(".a").nextUntil(".d");   // li.b and li.c

The family at a glance:

Direction One All Until
Down .children() .find(sel) none
Next .next() .nextAll() .nextUntil()
Previous .prev() .prevAll() .prevUntil()
Both n/a .siblings() n/a

Run each method from a chosen element and see which items light up.

Sideways methods on a list

A practical example: a definition list

Flat markup is where the sibling methods earn their keep. In a <dl>, each <dt> term is followed by its <dd> definition, with no wrapper to select. .nextUntil("dt") collects all the definitions that belong to a term.

Collapsible terms with nextUntil

Choosing a direction

When you write traversal code, say it in words first: "from the clicked button, go up to the card, then down to its title". Each step is one method: .closest(".card").find(".title"). If a chain starts to need four or five hops, add a class or a data- attribute to the element you want and select it directly, because structure changes more often than names do.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the difference between .children('li') and .find('li') on a <ul> with nested lists?

    .children() looks one level down, .find() searches all descendants.
  2. 2. Which call returns the label text node that .children() cannot reach?

    .contents() includes text and comment nodes, while the other methods return elements only.
  3. 3. What does $(this).addClass('active').siblings().removeClass('active') accomplish?

    .siblings() excludes the starting element, so only the neighbours lose the class.
  4. 4. For siblings a b c d e, what does $('.a').nextUntil('.d') return?

    nextUntil stops before the first element that matches, so d is not included.
  5. 5. What happens when you call .prev() on the first item of a list?

    There is no previous sibling, so the result is empty. Chaining on it is harmless.

Summary

  • .children() returns direct child elements; .find(selector) returns matching descendants at any depth; .contents() also returns text and comment nodes.
  • .siblings() returns the neighbours but never the starting element.
  • .next() / .prev() return one neighbour; .nextAll() / .prevAll() return all of them, nearest first.
  • .nextUntil() and .prevUntil() stop before the first match, ideal for flat markup such as dl or headings followed by paragraphs.
  • Empty results are safe to chain, and every method accepts an optional selector filter (.find() requires one).

Next: Working with Collections shows how to loop over a set, build arrays and manage the chain.