In this section, we will learn what the document getElementsByTagName() method is and how to use it in JavaScript.
What is document getElementsByTagName() Method in JavaScript?
The JavaScript document getElementsByTagName() method is a way of accessing elements in an HTML document using the name of HTML tags.
For example, if there are multiple <p>elements in a page and we want to change the font size for all of them no-matter what id or class attribute they might have, we can use this method and pass the “p” element as its argument to get the <p> elements and then use other methods in JavaScript to change their font-size.
document getElementsByTagName() Method Syntax:
document.getElementsByTagName(tag-name);
document getElementsByTagName() Method Parameters
The method takes one argument and that is the name of the tag we want to get a reference to.
document getElementsByTagName() Method Return Value
The return value of this method is a collection object that has a reference to each element with the specified tag name as the items of this collection.
Example: using document getElementsByTagName() Method in JavaScript
See the Pen using document getElementsByTagName() Method in JavaScript by Omid Dehghan (@odchan1) on CodePen.