getElementsByTagName() is a method which exists on the Document and Element objects and returns an HTMLCollection of descendant elements within the Document/Element given a tag name.
Let's implement our own Element.getElementsByTagName() that is similar but slightly different:
getElementsByTagName(document.body, 'div').Element.getElementsByTagName(), only descendants of the element argument are searched, not the element itself.Element**s, instead of an HTMLCollection of **Element**s.Do not use document.querySelectorAll() which will otherwise make the problem trivial.
const doc = new DOMParser().parseFromString(
`<div id="foo">
<span>Span</span>
<p>Paragraph</p>
<div id="bar">Div</div>
</div>`,
'text/html',
);
getElementsByTagName(doc.body, 'div');
// [div#foo, div#bar] <-- This is an array of elements.