getElementsByTagName method

The getElementsByTagName method allows you to get page elements by a tag name. With the retrieved elements, you can perform various manipulations: change their text, attributes, CSS styles, and so on.

Syntax

javascriptdocument.getElementsByTagName(tag name);

Example

Let's get all the paragraphs and change their text using the property innerHTML:

html<p>elem 1</p>
<p>elem 2</p>
<p>elem 3</p>
javascriptlet elems = document.getElementsByTagName('p');

for (let elem of elems) {
  elem.innerHTML = '!!!';
}

The code execution result:

html<p>!!!</p>
<p>!!!</p>
<p>!!!</p>

See also