getElementsByTagName
method returns all of the elements of the specified tag name as an array.
The following example gets all paragraph elements of the page and changes their content.
JSlet arr = document.getElementsByTagName("p");
for (let x = 0; x < arr.length; x++) {
arr[x].innerHTML = "Hi there";
}
HTML of the example<p>Hi</p>
<p>Hey</p>
<p>Hello</p>
<div>Hello there</div>
Note: We used the length
property of the array
to loop through all the selected elements in the above example