Select elements by tag name with getElementsByTagName()
document.getElementsByTagName('p') returns an HTMLCollection of matching elements in document order. Call the method on an element to search its descendants. The collection updates as the document changes.
Select matching tags in the whole document
Use numeric indexes and length to read the collection. With no match, collection[0] gives undefined, while collection.item(0) gives null. Pass '*' to match every descendant element. See getElementsByTagName().
{
const paragraphs = document.getElementsByTagName('p');
console.log(paragraphs.length);
if (paragraphs.length > 1) {
console.log(paragraphs[1].textContent);
}
}
Limit the search to one element
An element call does not include the element itself. It can find nested descendants such as a strong inside the second paragraph in a section. The first two snippets show the document-wide call and a scoped search. Run them separately in the console with the complete example open.
{
const group = document.querySelector('#group');
if (group) {
const local = group.getElementsByTagName('p');
const second = local[1];
if (second) {
const strong = second.getElementsByTagName('strong')[0];
console.log(strong?.textContent);
}
}
}
Compare a live HTMLCollection with static results
The live collection updates its indexes and length as elements are inserted or removed. If you remove elements while looping forward, the next index can shift and you may skip a match. Iterate backward or copy the list with Array.from() before removing elements.
{
const group = document.querySelector('#group');
const live = group.getElementsByTagName('p');
const snapshot = Array.from(live);
const added = document.createElement('p');
added.textContent = 'New paragraph';
group.append(added);
console.log(live.length);
console.log(snapshot.length);
}
querySelectorAll('p') returns a static NodeList. Like an array made with Array.from(), it retains the original list of elements. Both still reference the actual elements, so edits to the text of an original element are visible. See querySelectorAll().
Try the complete paragraph example
Save the full example in UTF-8 as tag-name-example.html and open it in a browser. There is one paragraph outside the section and two inside. The second paragraph contains a nested strong element.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Collect elements by tag name</title>
</head>
<body>
<h1>Collect paragraphs</h1>
<p>Outside paragraph</p>
<section id="group">
<p>First paragraph</p>
<p><strong>Second</strong> paragraph</p>
</section>
<button id="add" type="button">Add paragraph</button>
<button id="reset" type="button">Reset</button>
<pre id="output" aria-live="polite"></pre>
<script>
const group = document.querySelector('#group');
const allParagraphs = document.getElementsByTagName('p');
const groupParagraphs = group.getElementsByTagName('p');
const initialArray = Array.from(groupParagraphs);
const initialNodes = group.querySelectorAll('p');
const output = document.querySelector('#output');
function show() {
const second = groupParagraphs[1];
const strong = second.getElementsByTagName('strong')[0];
output.textContent = `Document p: ${allParagraphs.length}\nSection p: ${groupParagraphs.length}\nInitial array: ${initialArray.length}\nStatic NodeList: ${initialNodes.length}\nSecond paragraph: ${second.textContent.trim()}\nEmphasized text: ${strong.textContent}`;
}
document.querySelector('#add').addEventListener('click', () => {
const paragraph = document.createElement('p');
paragraph.textContent = `Added paragraph ${groupParagraphs.length + 1}`;
group.append(paragraph);
show();
});
document.querySelector('#reset').addEventListener('click', () => {
while (groupParagraphs.length > initialArray.length) {
groupParagraphs[groupParagraphs.length - 1].remove();
}
show();
});
show();
</script>
</body>
</html>
At first the document and section counts are 3 and 2. An addition changes them to 4 and 3, while the array and static list stay at 2. The emphasized text in the second paragraph remains the same. Reset removes the added paragraphs.




