Aral Code

Changing the Page Lesson 8 of 10

jQuery Traversing — parent, children, find, siblings and each

Move around the DOM tree with jQuery — parent, closest, children, find, siblings, next and prev — and loop over matched elements with each.

Traversing means moving from one element to related elements: its parent, children or siblings. It's essential when you click something inside a card or table row and need to change something else in the same card or row.

The family tree

<div class="card">            ← parent / ancestor of h3 and button
  <h3>Pandesal</h3>           ← sibling of p and button
  <p class="price">₱25</p>
  <button>Add</button>        ← child of .card
</div>

Traversing methods

MethodGoes to
.parent()The direct parent
.closest(sel)The nearest ancestor (or itself) matching sel
.children(sel)Direct children
.find(sel)Descendants at any depth
.siblings(sel)Elements with the same parent
.next(), .prev()The next / previous sibling

closest + find: the most useful pair

Example Product cards with quantity buttons
<script src="/vendor/jquery.min.js"></script>
<style>
  .card { display: inline-block; border: 1px solid #ddd; border-radius: 10px; padding: 10px; margin: 6px; width: 130px; text-align: center; font-family: sans-serif; }
  .qty { font-size: 22px; font-weight: bold; }
</style>
<div class="card" data-price="25"><h4>Pandesal</h4><button class="minus">−</button> <span class="qty">0</span> <button class="plus">+</button><p class="sub">₱0</p></div>
<div class="card" data-price="8"><h4>Kape</h4><button class="minus">−</button> <span class="qty">0</span> <button class="plus">+</button><p class="sub">₱0</p></div>
<div class="card" data-price="9"><h4>Itlog</h4><button class="minus">−</button> <span class="qty">0</span> <button class="plus">+</button><p class="sub">₱0</p></div>
<p id="grand" style="font-family:sans-serif"><strong>Total: ₱0</strong></p>
<script>
  $(".plus, .minus").on("click", function () {
    const card = $(this).closest(".card");           // go up to this button's card
    const qtyEl = card.find(".qty");                 // then down to its quantity
    let qty = Number(qtyEl.text()) + ($(this).hasClass("plus") ? 1 : -1);
    qty = Math.max(0, qty);
    qtyEl.text(qty);
    card.find(".sub").text("₱" + qty * card.data("price"));

    let total = 0;
    $(".card").each(function () {
      total += Number($(this).find(".qty").text()) * $(this).data("price");
    });
    $("#grand strong").text("Total: ₱" + total);
  });
</script>

One handler serves all three cards. closest(".card") finds the card that contains the clicked button, and find() searches only inside that card, so each card updates independently.

each(): looping over matches

Example Mark failing grades
<script src="/vendor/jquery.min.js"></script>
<table border="1" cellpadding="6" id="grades">
  <tr><td>Ana</td><td class="g">91</td></tr>
  <tr><td>Ben</td><td class="g">72</td></tr>
  <tr><td>Carla</td><td class="g">88</td></tr>
  <tr><td>Dan</td><td class="g">69</td></tr>
</table>
<p id="count"></p>
<script>
  let failing = 0;
  $(".g").each(function (index) {
    if (Number($(this).text()) < 75) {
      $(this).parent().css("background", "#fbe3e0");   // highlight the whole row
      failing++;
    }
  });
  $("#count").text(failing + " student(s) need remediation.");
</script>

siblings: one active item

Example Tabs with siblings()
<script src="/vendor/jquery.min.js"></script>
<style>.tab { padding: 6px 12px; border: 1px solid #ccc; cursor: pointer; display: inline-block; } .tab.active { background: #0f5c4d; color: #fff; }</style>
<div><span class="tab active">Q1</span><span class="tab">Q2</span><span class="tab">Q3</span><span class="tab">Q4</span></div>
<p id="shown">Showing Q1 grades</p>
<script>
  $(".tab").on("click", function () {
    $(this).addClass("active").siblings().removeClass("active");
    $("#shown").text("Showing " + $(this).text() + " grades");
  });
</script>

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What does .closest(".card") do?
2. What's the difference between children() and find()?
3. What does .siblings().removeClass("active") do after clicking a tab?
4. Which method loops over each matched element?

By Sir Reyel · Last updated 2026-12-04