Skip to curriculum
Jump to the editor

16.5. Implement getElementsByTagName

Medium
Topics

This question is in

16. DOM: finding elements
15–25 min

getElementsByTagName is one of the oldest ways to search the DOM: table.getElementsByTagName("tr") gives every row, at any depth. Writing it yourself is the simplest real tree walk there is, and it shows what "document order" means: the order in which the elements' start tags appear in the HTML.

Write getElementsByTagName(root, tagName) in by-tag.js. It returns the same elements, in the same order, as root.getElementsByTagName(tagName), but as an array.

Signature

JAVASCRIPT
function getElementsByTagName(root, tagName) // returns Element[]
  • root: an element, the document, or a document fragment.
  • tagName: a tag name such as "li" or "my-card", or "*" for every element.
  • Returns the matching descendants of root, in document order. root itself is never included.

Requirements

  1. The result is an array of every matching descendant of root, at any depth, and never root itself.
  2. The order is document order: an element comes before the elements inside it, and those come before its next sibling.
  3. HTML elements match in any case: "LI" and "li" find the same elements.
  4. Elements that are not in the HTML namespace, such as SVG's linearGradient, match only with the exact case.
  5. "*" matches every element. Text and comment nodes never match anything.
  6. The name is a tag name, not a CSS selector: "div p" or "span.x" matches nothing.
  7. It works when root is the document, a detached element or a fragment, and it never looks inside template content or shadow roots.
  8. The result is a snapshot: later changes to the page do not change it. Nothing matching gives [].
  9. It works for trees 2,000 levels deep or 5,000 children wide, and with every built-in search method removed.

Examples

JAVASCRIPT
// <ul id="menu">
//   <li id="a">Home</li>
//   <li id="b">Docs
//     <ul><li id="c">API</li></ul>
//   </li>
//   <li id="d">Blog</li>
// </ul>
const menu = document.getElementById("menu");
getElementsByTagName(menu, "li");  // [li#a, li#b, li#c, li#d]: c comes before d
getElementsByTagName(menu, "UL");  // [the inner ul]: not menu itself
getElementsByTagName(menu, "p");   // []
JAVASCRIPT
getElementsByTagName(menu, "*");      // every element inside menu, in document order
getElementsByTagName(menu, "ul li");  // []: a tag name, not a selector
JAVASCRIPT
// <svg><linearGradient id="g"></linearGradient></svg>
getElementsByTagName(document, "linearGradient"); // [the gradient]
getElementsByTagName(document, "lineargradient"); // []

Environment

  • Plain JavaScript in a browser page; the tests call the global getElementsByTagName from by-tag.js.
  • Not allowed: getElementsByTagName, getElementsByTagNameNS, getElementsByClassName, querySelector and querySelectorAll. Two tests remove them all while they run. children, localName and namespaceURI are allowed.

Hints

  1. Searching inside a child is the same problem as searching inside root. How can the function use that?
  2. Which property of an element gives its tag name in the form the HTML parser stored it?

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…