0
Hours
0
Minutes
0
Seconds
You need Login/Signup to run/submit the code.

How to implement getElementsByTagName? | Frontend Problem Solving | JavaScript Interview Question

@Devtools Tech
247

In this question, the candidate needs to build a function called getElementsByTagName that mimics the behaviour of the method with the same name provided by the DOM interface.

Description

The getElementsByTagName method of Document interface returns a live HTMLCollection of elements with the given tag name.

When called as Element.getElementsByTagName() then all descendants of the specified element are searched, but not the element itself. The returned list is live, which means it updates itself with the DOM tree automatically.

Read more about getElementsByTagName here.

Syntax

getElementsByTagName(tagName)

Arguments

  • tagName (String): Tag name to be searched

Return value

A live HTMLCollection of found elements.

Examples

  • Get all p elements:
getElementsByTagName("p");
  • Get all div elements inside a section with container as id:
getElementById('container').getElementsByTagName("div");

Submission

Start the timer, complete your solution, test your solution against the test cases provided by the platform, and submit it. Ideally, you should finish this question within 60 mins.

Share your solution with us on twitter or linkedin.

Loading IDE...