FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Medium

Get Element By Tag Name

20mins

Implement a function getElementByTagName(root, tagName) that returns an array of all descendant elements of the given root element matching the specified tag name.

The function should:

  • Traverse all descendants of the root element recursively.
  • Match the tag name case-insensitively.
  • Return an array of matching elements.

Example

const doc = new DOMParser().parseFromString( `<div> <span>One</span> <p>Two</p> <div><span>Three</span></div> </div>`, 'text/html' ); getElementByTagName(doc.body, 'span'); // Returns [<span>One</span>, <span>Three</span>]

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests