16.1. Ways to select and loop over elements
Topics
This question is in
16. DOM: finding elementsMost 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
function itemTexts(list) // returns string[]
function checkedValues(form) // returns string[]
function removeByClass(root, className) // returns numberitemTexts(list):listis a<ul>or<ol>. Returns the trimmed text of each of its element children, in order.checkedValues(form): returns thevalueof every checkbox insideformthat is checked right now, in document order.removeByClass(root, className): removes every element insiderootthat has the classclassName, and returns how many matched.
Requirements
itemTextsreturns one entry per element child of the list, in order, with the text trimmed.itemTextsignores the whitespace text nodes and comments between items.itemTextscounts only the list's own children: the items of a nested list are part of their parent item.checkedValuesreads the current checked state, so a box the user ticked or unticked after the page loaded counts as it is now.checkedValuesreturns only checkboxes (not radio buttons or other inputs), at any depth insideform.removeByClassremoves every match, including matches that sit side by side.removeByClassmatches whole class names (itemdoes not matchitemsoritem-x), including names such asmd:hiddenor2col.removeByClassnever removesrootitself or anything outside it, and returns the number of matches (0 when there are none).- Every helper returns an empty array or 0 when there is nothing to find.
Examples
// <ul id="nav">
// <li>Home</li>
// <li> Docs </li>
// </ul>
itemTexts(document.getElementById("nav")); // ["Home", "Docs"]// <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"]// <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,checkedValuesandremoveByClassfromselect.js. - Each test builds its own elements inside
document.bodyand removes them afterwards. - Every built-in DOM method is allowed here: the point is to pick the right one.
Hints
- A list's
childNodesand itschildrengive different lengths for the same markup. Why? - 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