document.getElementsByTagName() is an instance method that returns every element with a given tag name (see MDN Document: getElementsByTagName()). Learn the live HTMLCollection, the * wildcard, element-scoped searches, how it compares to querySelectorAll, and five try-it labs.
01
Kind
Instance method
02
Arg
tag name / *
03
Returns
HTMLCollection
04
Live?
Yes
05
Order
tree order
06
Status
Baseline
Fundamentals
Introduction
Need every <p>, <li>, or <img> on the page? Pass the tag name to document.getElementsByTagName() and get a live list.
MDN: the method returns an HTMLCollection of elements with the given tag name. The complete document is searched, including the root node. The collection is live — it stays in sync with the DOM without calling the method again.
💡
Think: “give me all paragraphs”
1) Call document.getElementsByTagName("p") 2) Get a live HTMLCollection in tree order 3) Read length, [0], or loop 4) Or pass "*" to match every element (MDN)
📄
Document vs Element (MDN)
document.getElementsByTagName() searches the whole document. Element.getElementsByTagName() is functionally identical but starts at a specific parent — perfect for counting tags inside one panel.
Use * sparingly on large pages — it can be a long live list. Prefer a specific tag when you already know the element type.
Example 4 — Live collection updates
Append another matching tag and watch length grow without re-querying.
JavaScript
const list = document.getElementsByTagName("li");
console.log("before:", list.length);
const extra = document.createElement("li");
extra.textContent = "New item";
document.querySelector("ul").appendChild(extra);
console.log("after:", list.length); // live list updated (MDN)
Document.getElementsByTagName() is Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Document.getElementsByTagName()
Live HTMLCollection of elements matching a tag name across all major browsers.
BaselineWidely available
Google ChromeSupported
Yes
Mozilla FirefoxSupported
Yes
Apple SafariSupported
Yes
Microsoft EdgeSupported
Yes
OperaSupported
Yes
Internet ExplorerSupported (legacy)
Yes
getElementsByTagName()Wide
Bottom line: Use getElementsByTagName for simple tag lists. Prefer querySelectorAll for complex selectors or a static NodeList.
Wrap Up
Conclusion
document.getElementsByTagName(name) gathers every matching tag into a live HTMLCollection. Pass a tag name or *, optionally scope to a parent element, and remember the list stays synced as the DOM changes.
Scope searches with an element root for local counts (MDN)
Remember the collection is live and ordered by the tree (MDN)
Convert with Array.from when you need Array helpers
Reach for getElementsByTagNameNS for camel-case SVG (MDN)
❌ Don’t
Treat the return value as one element without [0]
Forget HTML lowercases the argument (MDN)
Overuse * on huge documents without need
Assume Array methods exist without converting first
Skip querySelectorAll when you need complex CSS filters
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about getElementsByTagName()
Live tag matches as an HTMLCollection.
5
Core concepts
📝01
Returns
HTMLCollection
MDN
🔄02
Live
auto-updates
MDN
🎯03
Wildcard
* = all
MDN
⚡04
Scope
doc or element
MDN
🛡05
Status
Baseline
2015
❓ Frequently Asked Questions
MDN: Document.getElementsByTagName() returns an HTMLCollection of elements with the given tag name. The complete document is searched, including the root node.
No. MDN marks Document.getElementsByTagName() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
A live HTMLCollection of found elements in the order they appear in the tree (MDN). It updates automatically as the DOM changes.
MDN: the special string * represents all elements.
Yes. MDN: Element.getElementsByTagName() is functionally identical but starts the search at a specific element in the DOM tree.
MDN: on an HTML document, getElementsByTagName() lower-cases its argument. For camel-case SVG elements, use document.getElementsByTagNameNS().
Did you know?
MDN’s nested #div1 / #div2 demo shows that paragraphs inside the inner box still count toward the outer box — because getElementsByTagName walks all descendants, not just direct children.