Skip to curriculum
Jump to the editor

16.1. Ways to select and loop over elements

Easy
Topics

This question is in

16. DOM: finding elements
10–20 min

Most DOM code starts the same way: find some elements, then loop over them. The browser gives several ways to do both, and they do not behave the same. children holds only elements while childNodes also holds text and comments; querySelectorAll returns a fixed list while getElementsByClassName returns a live one that changes as the page changes. Picking the wrong one gives bugs that only show up with real markup: a blank entry for the whitespace between tags, or a loop that removes every second item and leaves the rest.

Write three small helpers in select.js.

Signature

JAVASCRIPT
function itemTexts(list)                // returns string[]
function checkedValues(form)            // returns string[]
function removeByClass(root, className) // returns number
  • itemTexts(list): list is a <ul> or <ol>. Returns the trimmed text of each of its element children, in order.
  • checkedValues(form): returns the value of every checkbox inside form that is checked right now, in document order.
  • removeByClass(root, className): removes every element inside root that has the class className, and returns how many matched.

Requirements

  1. itemTexts returns one entry per element child of the list, in order, with the text trimmed.
  2. itemTexts ignores the whitespace text nodes and comments between items.
  3. itemTexts counts only the list's own children: the items of a nested list are part of their parent item.
  4. checkedValues reads the current checked state, so a box the user ticked or unticked after the page loaded counts as it is now.
  5. checkedValues returns only checkboxes (not radio buttons or other inputs), at any depth inside form.
  6. removeByClass removes every match, including matches that sit side by side.
  7. removeByClass matches whole class names (item does not match items or item-x), including names such as md:hidden or 2col.
  8. removeByClass never removes root itself or anything outside it, and returns the number of matches (0 when there are none).
  9. Every helper returns an empty array or 0 when there is nothing to find.

Examples

JAVASCRIPT
// <ul id="nav">
//   <li>Home</li>
//   <li> Docs </li>
// </ul>
itemTexts(document.getElementById("nav")); // ["Home", "Docs"]
JAVASCRIPT
// <form id="prefs">
//   <input type="checkbox" value="mail" checked>
//   <input type="checkbox" value="sms">
// </form>
const prefs = document.getElementById("prefs");
prefs.elements[1].checked = true;   // the user ticks "sms"
checkedValues(prefs);               // ["mail", "sms"]
JAVASCRIPT
// <ul id="todo">
//   <li class="done">a</li><li class="done">b</li><li class="done">c</li><li>d</li>
// </ul>
removeByClass(document.getElementById("todo"), "done"); // 3
// the list now holds only "d"

Environment

  • Plain JavaScript in a browser page; the tests call the globals itemTexts, checkedValues and removeByClass from select.js.
  • Each test builds its own elements inside document.body and removes them afterwards.
  • Every built-in DOM method is allowed here: the point is to pick the right one.

Hints

  1. A list's childNodes and its children give different lengths for the same markup. Why?
  2. If you remove item 0 of a live collection, which item is at index 0 now?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…