TExcelTAD
Home › jQuery › jQuery Descendants

jQuery Traversing: Descendants

Descendants are the elements nested inside another element: its children, their children, and so on. jQuery has two main methods for moving down: .children() for one level and .find() for any depth.

Method Returns
.children(selector?) Direct children only (element nodes), optionally filtered
.find(selector) All descendants at any depth matching selector (required argument)
.contents() Direct children including text and comment nodes (and an iframe's document)

children() vs find()

html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <style>
    body { font-family: sans-serif; font-size: 14px; }
    #menu, #menu ul { border: 1px solid #ccc; padding: 4px 4px 4px 24px; }
    .hl { background: #fff176; }
  </style>
</head>
<body>
  <ul id="menu">
    <li>Home</li>
    <li>Products
      <ul>
        <li>Coffee</li>
        <li>Tea</li>
      </ul>
    </li>
    <li>Contact</li>
  </ul>
  <button id="c">children("li")</button>
  <button id="f">find("li")</button>
  <button id="ff">find("ul li")</button>

  <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  <script>
    function show($set, label) {
      $(".hl").removeClass("hl");
      $set.addClass("hl");
      const texts = $set.map((i, el) => $(el).contents().first().text().trim()).get();
      console.log(label, "→", $set.length, texts);
    }
    $("#c").on("click", () => show($("#menu").children("li"), "children('li')"));
    $("#f").on("click", () => show($("#menu").find("li"), "find('li')"));
    $("#ff").on("click", () => show($("#menu").find("ul li"), "find('ul li')"));
  </script>
</body>
</html>
  • children("li") → 3 items (Home, Products, Contact).
  • find("li") → 5 items, including the nested Coffee and Tea.
  • find("ul li") → only Coffee and Tea: the selector is evaluated inside #menu, so it needs a ul within #menu.

Note: .find() requires a selector. To get all descendants use .find("*").

find() is a scoped search

$("#sidebar").find("a") is the same as $("#sidebar a"), but .find() is more flexible because the starting point can be any jQuery object, such as $(this) inside a handler. It is also the cleanest way to write reusable components that only touch their own markup:

html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <style>
    body { font-family: sans-serif; font-size: 14px; }
    .counter { display: inline-flex; align-items: center; gap: 6px; border: 1px solid #ccc; padding: 6px 10px; border-radius: 6px; margin: 4px; }
    .counter output { min-width: 24px; text-align: center; font-weight: bold; }
  </style>
</head>
<body>
  <div class="counter" data-label="Adults"><span>Adults</span><button class="minus">−</button><output>2</output><button class="plus">+</button></div>
  <div class="counter" data-label="Children"><span>Children</span><button class="minus">−</button><output>0</output><button class="plus">+</button></div>
  <div class="counter" data-label="Pets"><span>Pets</span><button class="minus">−</button><output>1</output><button class="plus">+</button></div>

  <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  <script>
    $(".counter").each(function () {
      const $c = $(this);
      const $out = $c.find("output");       // only THIS counter's output
      $c.find(".plus").on("click", () => update(1));
      $c.find(".minus").on("click", () => update(-1));

      function update(delta) {
        const n = Math.max(0, Number($out.text()) + delta);
        $out.text(n);
        $c.find(".minus").prop("disabled", n === 0);
        console.log($c.data("label") + ":", n);
      }
      $c.find(".minus").prop("disabled", Number($out.text()) === 0);
    });
  </script>
</body>
</html>

Each counter works independently even though they share the same class names — every lookup starts from $c.

contents(): text nodes too

.children() ignores text. .contents() returns all child nodes, including text nodes, which lets you change loose text that is not wrapped in a tag:

html · try it yourself
<!DOCTYPE html>
<html>
<body style="font-family: sans-serif">
  <p id="p">Hello <b>bold</b> world <i>!</i></p>
  <button id="go">Wrap the loose text</button>

  <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  <script>
    $("#go").on("click", function () {
      const $all = $("#p").contents();
      console.log("children():", $("#p").children().length, "| contents():", $all.length);

      $all
        .filter(function () { return this.nodeType === 3 && this.nodeValue.trim() !== ""; })
        .wrap("<mark></mark>");

      console.log("html:", $("#p").html());
    });
  </script>
</body>
</html>

nodeType === 3 identifies text nodes. .wrap() puts each one inside a new <mark> element.

Related methods

Method What it does
.has(selector) Keeps elements that contain a matching descendant
.wrap(html) / .wrapInner(html) / .wrapAll(html) Wrap each element / its content / the whole set
.first() / .last() First / last element of the found set
js
$("li").has("ul").addClass("has-submenu");   // items that contain a nested list

Tip: .find() on a cached object is faster than repeating a long global selector: store const $form = $("#checkout") once, then use $form.find(...) everywhere.

Test yourself

Which method returns only the direct children of an element?

  1. .find()
  2. .children()
  3. .contents()
  4. .descendants()

Which method also returns text nodes?

  1. .children()
  2. .find("*")
  3. .contents()
  4. .text()

Inside a handler, how do you get only the .price elements within the clicked card $card?

  1. $(".price")
  2. $card.find(".price")
  3. $card.parents(".price")
  4. $card.siblings(".price")