JavaScript Document getElementsByTagName() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

document.getElementsByTagName(name) returns a live HTMLCollection 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)
CallWhat 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

ParameterDescription
nameRequired. Tag name string; * means all elements (MDN)

Return Value

Return ValueDescription
*Live HTMLCollection in tree order

JavaScript Examples

Example 1 — Count All Paragraphs

Collect every <p> in the document and read .length.

JavaScript
const allParas = document.getElementsByTagName("p");
console.log(allParas.length);
console.log(allParas[0].tagName); // "P"
Try It Yourself

How It Works

  1. Pass the tag. "p" matches paragraph elements (MDN).
  2. Search the document. Includes the root and all descendants.
  3. Read the collection. .length is the count; [0] is the first match in tree order.
  4. Live list. You get an HTMLCollection that stays in sync with the DOM.

Example 2 — Search Inside One Element

Call the same method on a parent to limit the search to its descendants (MDN).

JavaScript
const div1 = document.getElementById("div1");
const div1Paras = div1.getElementsByTagName("p");
console.log(div1Paras.length); // only descendants of #div1
Try It Yourself

How It Works

  1. Pick a root. getElementById("div1") finds the parent.
  2. Search that subtree. Element.getElementsByTagName starts at that element (MDN).
  3. Nested matches count. Paragraphs inside nested #div2 are included.
  4. Outer nodes stay out. Paragraphs outside #div1 are not in this list.

Example 3 — Live HTMLCollection

Add another matching tag and watch length grow without calling the method again.

JavaScript
const list = document.getElementsByTagName("li");
console.log("before:", list.length);

const extra = document.createElement("li");
extra.textContent = "added";
document.querySelector("ul").appendChild(extra);

console.log("after:", list.length); // live collection grew (MDN)
Try It Yourself

How It Works

  1. Store the collection once. Keep a reference to the live HTMLCollection.
  2. Add a match. A new <li> enters the document.
  3. Length updates. MDN: the collection stays in sync without another call.
  4. Static alternative. querySelectorAll("li") would stay frozen at query time.

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.

Baseline Widely available
Google Chrome Supported
Yes
Mozilla Firefox Supported
Yes
Apple Safari Supported
Yes
Microsoft Edge Supported
Yes
Opera Supported
Yes
Internet Explorer Supported (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().

Next: getElementsByTagNameNS()

Learn how to collect elements by namespace URI and local name — useful for SVG.

getElementsByTagNameNS() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful