Skip to curriculum
Jump to the editor

16.6. Implement getElementsByClassName

Medium
Topics

This question is in

16. DOM: finding elements
15–25 min

Class names are how most code labels elements: card, active, is-open. root.getElementsByClassName("card active") finds every element inside root that has all the classes listed, in document order. Writing it yourself means two small but exact jobs: split the argument into names the way the browser does, and walk the tree checking each element's class list.

Write getElementsByClassName(root, classNames) in by-class.js. It returns the same elements, in the same order, as root.getElementsByClassName(classNames), but as an array.

Signature

JAVASCRIPT
function getElementsByClassName(root, classNames) // returns Element[]
  • root: an element, the document, or a document fragment.
  • classNames: one or more class names separated by whitespace, such as "card" or "card active".
  • Returns the descendants of root that have every one of the classes, in document order. root itself is never included.

Requirements

  1. The result is an array of every matching descendant of root, at any depth, in document order, and never root itself.
  2. Class names match whole names only: item does not match items, item-x or my-item.
  3. With several names, an element must have all of them; the order of the names does not matter, and a name given twice counts once.
  4. The names are separated by any run of ASCII whitespace (space, tab, line feed, form feed, carriage return), at the start and end too. A non-breaking space is part of a name, not a separator.
  5. An empty or blank argument matches nothing.
  6. Class names are case-sensitive, and names with characters like :, / or [ work like any other.
  7. SVG elements are searched like HTML elements.
  8. It works when root is the document, a detached element or a fragment, and it never looks inside template content or shadow roots.
  9. The result is a snapshot that later changes to the page do not alter. Nothing matching gives [].
  10. It works on large trees, and with every built-in search method removed.

Examples

JAVASCRIPT
// <ul id="list">
//   <li id="a" class="item done">Write</li>
//   <li id="b" class="item">Test</li>
//   <li id="c" class="done item urgent">Ship</li>
// </ul>
const list = document.getElementById("list");
getElementsByClassName(list, "item");          // [li#a, li#b, li#c]
getElementsByClassName(list, "done item");     // [li#a, li#c]
getElementsByClassName(list, "  urgent\n");    // [li#c]
JAVASCRIPT
getElementsByClassName(list, "Item");   // []: case-sensitive
getElementsByClassName(list, "it");     // []: whole names only
getElementsByClassName(list, "");       // []

Environment

  • Plain JavaScript in a browser page; the tests call the global getElementsByClassName from by-class.js.
  • Not allowed: getElementsByClassName, getElementsByTagName, getElementsByTagNameNS, querySelector and querySelectorAll. Two tests remove them all while they run. children and classList are allowed.
  • The page is in standards mode (it has a doctype), where class names are case-sensitive.

Hints

  1. What should "has every one of these classes" be when the list of classes is empty, and what does the built-in return?
  2. Which characters does \s match in a regular expression, and are they all separators here?

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…