document.getElementsByTagName(name) returns a liveHTMLCollection of every element with that tag name (MDN). The whole document is searched, including the root. Use "*" to match all elements.
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax
Call document.getElementsByTagName. Start with this line.
JavaScript
document.getElementsByTagName(name)
Call
What it does
document.getElementsByTagName(name)
Primary signature.
document.getElementsByTagName(...)
Call with the arguments your case needs.
Built into the browser. Call getElementsByTagName(...) on document — no import required.
Library declarations
JavaScript
document.getElementsByTagName(name)
Parameters
Parameter
Description
name
Required. Tag name string; * means all elements (MDN)
Return Value
Return Value
Description
*
Live HTMLCollection in tree order
JavaScript Examples
Example 1 — Count All Paragraphs
Collect every <p> in the document and read .length.
Store the collection once. Keep a reference to the live HTMLCollection.
Add a match. A new <li> enters the document.
Length updates. MDN: the collection stays in sync without another call.
Static alternative.querySelectorAll("li") would stay frozen at query time.
Compatibility
Browser Support
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: Safe for everyday tag lookups. Prefer querySelectorAll for static CSS-based lists, or getElementsByTagNameNS for SVG namespaces. See the MDN Document.getElementsByTagName() reference.
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().