Jump to the editorJAVASCRIPT
JAVASCRIPT JAVASCRIPT JAVASCRIPT
16.5. Implement getElementsByTagName
Medium
Topics
This question is in
16. DOM: finding elementsgetElementsByTagName 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
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.rootitself is never included.
Requirements
- The result is an array of every matching descendant of
root, at any depth, and neverrootitself. - The order is document order: an element comes before the elements inside it, and those come before its next sibling.
- HTML elements match in any case:
"LI"and"li"find the same elements. - Elements that are not in the HTML namespace, such as SVG's
linearGradient, match only with the exact case. "*"matches every element. Text and comment nodes never match anything.- The name is a tag name, not a CSS selector:
"div p"or"span.x"matches nothing. - It works when
rootis the document, a detached element or a fragment, and it never looks insidetemplatecontent or shadow roots. - The result is a snapshot: later changes to the page do not change it. Nothing matching gives
[]. - It works for trees 2,000 levels deep or 5,000 children wide, and with every built-in search method removed.
Examples
// <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"); // []getElementsByTagName(menu, "*"); // every element inside menu, in document order
getElementsByTagName(menu, "ul li"); // []: a tag name, not a selector// <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
getElementsByTagNamefromby-tag.js. - Not allowed:
getElementsByTagName,getElementsByTagNameNS,getElementsByClassName,querySelectorandquerySelectorAll. Two tests remove them all while they run.children,localNameandnamespaceURIare allowed.
Hints
- Searching inside a child is the same problem as searching inside
root. How can the function use that? - 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…