Chapter 6 · Core
Moving around the page
So far you've found elements by their id, class or tag. In this chapter you'll learn to start from one element and step to the ones around it: up to its parent, down to its children, or sideways to its neighbours. This is how a "Remove" button finds the card it belongs to.
Why move around?
Imagine a shopping cart with three items. Each item has its own Remove button, and all three buttons have the same class:
<ul id="cart">
<li>Pen <button class="remove">Remove</button></li>
<li>Ruler <button class="remove">Remove</button></li>
<li>Eraser <button class="remove">Remove</button></li>
</ul>When a Remove button is clicked, you want to remove its own item, not all of them. Giving every item an id would be tiring, and impossible for items added later. Instead, you start from the button that was clicked and step up to the <li> around it. That's what this chapter is about.
The page is a family tree
Elements inside other elements are like a family. People who build websites even use family words for them:
<ul> is the parent of each <li>.<li> is a child of the <ul>.<li> items are siblings.<body>.Here's the cart as a tree, inside a card. We start from the Ruler item, and each arrow shows where a method takes you:
.parent() goes one step up, .closest(".card") keeps going up until it finds a match, .prev() and .next() go sideways, and .find() searches inside.Every method in this chapter gives you a new Nyx result, so you can keep chaining: button.closest("li").find(".price").text().
Going up
.parent()The element directly around it: exactly one step up..closest("li")Go up step by step until an element matches, and stop there. It checks the element itself first..parents()Every ancestor, all the way up to <html>. Give it a selector to keep only the ones that match: .parents(".box")..closest() is the one you'll use most, because you don't need to count steps. Even if you later wrap the button in a <span>, .closest("li") still finds the item.
- Pen
- Ruler
- Eraser
nyx("#m-cart").on("click", ".m-remove", ...)Listen for clicks on any Remove button in the cart (event delegation, from Events).button.closest("li")From the clicked button, go up to the nearest<li>: its own item.item.remove();Remove just that item.
Press Run, then remove items one by one. Try changing .closest("li") to .parent(): it works too, because the button sits directly inside the li.
Going down
.children()The elements directly inside it, one level down only..children(".done")Only the children that match the selector..find(".price")Search everywhere inside it, at any depth.Science kit
Price: ₹450
In stock: 12
The card has three children: the heading and two paragraphs. The prices are inside the paragraphs, a level deeper, so .children(".m-price") wouldn't find them, but .find() does.
Going sideways
.next()The sibling right after it..prev()The sibling right before it..siblings()All the other children of the same parent, before and after..siblings(".done")Only the siblings that match.- Asha
- Ravi
- Meera
- John
Asha (before Ravi) turns red and Meera (after Ravi) turns teal. John is a sibling too, but not the very next one.
Narrowing a group
These methods start from a group you already found and keep only some of it:
.filter(".sale")Keep only the elements that match..not(".sold-out")Keep only the elements that don't match..has("img")Keep only the elements that have a match somewhere inside them..is(".active")A question, not a group: true if any of them match..index()A number: its position among its siblings, counting from 0.- Maths guide
- Atlas
- Dictionary
- Story book
Three books are on sale, so three turn bold. The Dictionary is sold out, so only two get highlighted.
.filter() can also take a function. Return true to keep an element:
// Keep only the items with more than 5 letters
nyx("#m-books li").filter((item) => item.text().length > 5);The active class, by hand
In Events you met .pick(), which makes one item active. Now that you know .siblings(), you can see exactly how it works:
item.addClass("active");Give the clicked item the class.item.siblings().removeClass("active");Step sideways to every other item, and take the class off them.
That's the whole idea: two lines. item.pick("active") does the same thing in one, and also sets aria-current for screen readers. Now you know what's happening inside it.
Putting it together
A cart where every item has its own + and − buttons. Each button finds its own item with .closest(), then the number inside it with .find().
- Pen · ₹20 × 1
- Notebook · ₹45 × 2
Total: ₹110
function updateTotal() { ... }A named function that adds up the cart. Naming it means we can run it whenever we like, by writingupdateTotal().item.data("price") * Number(...)Price times quantity.Number()turns the text"2"into the number2.button.closest("li")From the clicked button, up to its own item.item.find(".m-qty")From the item, down to its quantity.button.is(".m-more")Was it the + button?.is()answerstrueorfalse.
Common mistakes
.parent() two levels awayIt only goes one step. Use .closest("li") to keep going until it finds a match..children(".price") finds nothing.children() only looks one level down. Use .find(".price") to search at any depth..next(".done") finds nothingIt only checks the very next sibling, and gives it to you if it matches. It doesn't keep looking further along.nyx(".remove").closest("li")This finds the items of every Remove button. Inside a click listener, start from the button Nyx gives you..closest("#cart li")Fine, but usually .closest("li") is enough. Keep selectors short.Exercise
Fill in the blanks, then press Check answers.
1. From a button, go up to the nearest .card.
button.(".card");
2. Go exactly one step up from #price.
nyx("#price").();
3. Search for every img anywhere inside #gallery.
nyx("#gallery").("img");
4. Get the element right after #question.
nyx("#question").();
5. Take the class active off every other item.
item.().removeClass("active");
6. From all li items, keep only those that don't have the class done.
nyx("li").(".done");
Remember
.parent() is one step, .closest("li") goes until it finds a match..children() is one level, .find() searches at any depth..next(), .prev() and .siblings()..filter(), .not() and .has(). Ask with .is()..pick() is just .addClass() plus .siblings().removeClass().