JavaScript Element getElementsByTagName() Method

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline
Instance method

What You’ll Learn

Element.getElementsByTagName() is an instance method that finds descendant elements by tag name and returns a live HTMLCollection. Learn table cell lookups, the "*" wildcard, comparison with getElementsByClassName() and querySelectorAll(), and five try-it labs aligned with MDN.

01

Kind

Instance method

02

Returns

HTMLCollection

03

Args

tag name

04

Scope

Descendants

05

Wildcard

"*"

06

Status

Baseline widely

Introduction

Before querySelectorAll was everywhere, getElementsByTagName() was the standard way to grab every <p>, <li>, or <td> inside a section of the page. It still works well for simple tag-based lookups scoped to a parent element.

The return value is a live HTMLCollection. MDN notes it updates automatically when the DOM changes, so you usually do not need to call the method again after each mutation.

💡
Beginner tip

In HTML documents, tag names are lower-cased before searching (MDN). For camel-cased SVG elements in HTML, use getElementsByTagNameNS() instead. For complex CSS selectors, prefer querySelectorAll().

This page is part of JavaScript Element. Document.getElementsByTagName() works the same way from the document root (MDN).

Understanding the getElementsByTagName() Method

Calling el.getElementsByTagName(tagName) walks the subtree under el and collects every descendant whose tag name matches.

  • It is an instance method on Element.
  • tagName — the qualified name to look for; use "*" for all (MDN).
  • Returns a live HTMLCollection in document order.
  • Searches descendants only—not the element itself.
  • In HTML, tag names are lower-cased before matching (MDN).
  • Also available on Document for whole-page searches (MDN).

📝 Syntax

General form of Element.getElementsByTagName (MDN):

JavaScript
getElementsByTagName(tagName)

Parameters

tagName — a string with the qualified name to look for. The special string "*" matches all descendant elements (MDN).

Return value

A live HTMLCollection of matching elements in document order. Returns an empty collection when nothing matches (MDN).

Common patterns

JavaScript
// All paragraphs in a section
const paragraphs = section.getElementsByTagName("p");

// Table cells (MDN example)
const table = document.getElementById("forecast-table");
const cells = table.getElementsByTagName("td");

// Every descendant
const all = container.getElementsByTagName("*");

console.log(paragraphs.length);
console.log(cells[0]);

⚡ Quick Reference

GoalCode
Find by tagel.getElementsByTagName("p")
All descendantsel.getElementsByTagName("*")
Table cellstable.getElementsByTagName("td")
Count matchescollection.length
Static alternativeel.querySelectorAll("p")
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about Element.getElementsByTagName().

Returns
HTMLCollection

Live list

Baseline
widely

Since Jul 2015

Wildcard
"*"

All tags

Modern
querySelAll

Static alt

📋 DOM collection APIs

getElementsByTagName()getElementsByClassName()querySelectorAll()
ReturnsLive HTMLCollectionLive HTMLCollectionStatic NodeList
Matches byTag nameClass name(s)CSS selector
Wildcard"*" for allNoFull selector syntax
Best forSimple tag lookupsSimple class lookupsComplex selectors
On DocumentYesYesYes

Examples Gallery

Examples follow MDN Element.getElementsByTagName() patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

MDN: read table cells and other elements by tag name.

Example 1 — Table <td> Cells

MDN forecast-table pattern: loop every cell in a table.

JavaScript
const table = document.getElementById("forecast-table");
const cells = table.getElementsByTagName("td");

for (const cell of cells) {
  const status = cell.getAttribute("data-status");
  console.log(status);
}
Try It Yourself

How It Works

Only <td> elements inside the table are returned. The <table> element itself is not included.

Example 2 — All <p> in a Section

Scoped tag search inside a container element.

JavaScript
const article = document.getElementById("article");
const paragraphs = article.getElementsByTagName("p");

console.log(paragraphs.length);
console.log(paragraphs[0].textContent);
Try It Yourself

How It Works

Paragraphs outside #article are ignored. Use lowercase tag names in HTML documents (MDN).

📈 Practical Patterns

Wildcard, live collections, and comparison with modern selectors.

Example 3 — Wildcard "*"

MDN: "*" returns every descendant element.

JavaScript
const container = document.getElementById("widget");
const all = container.getElementsByTagName("*");

console.log(all.length);
// Every descendant tag inside #widget
Try It Yourself

How It Works

The wildcard is useful when you need a count or iteration over every nested element without specifying each tag.

Example 4 — Live Collection Updates

MDN: the collection reflects DOM changes without calling the method again.

JavaScript
const list = document.getElementById("list");
const items = list.getElementsByTagName("li");

console.log(items.length); // e.g. 2

const newItem = document.createElement("li");
newItem.textContent = "New";
list.appendChild(newItem);

console.log(items.length); // 3 — same collection, updated
Try It Yourself

How It Works

Unlike querySelectorAll, you keep one live reference and it grows or shrinks with the subtree.

Example 5 — vs querySelectorAll()

Live HTMLCollection vs static NodeList.

JavaScript
const box = document.getElementById("box");

const byTag = box.getElementsByTagName("span");
const byQuery = box.querySelectorAll("span");

box.insertAdjacentHTML("beforeend", "Added");

console.log(byTag.length);    // increased
console.log(byQuery.length);  // unchanged snapshot
Try It Yourself

How It Works

Choose getElementsByTagName when you want live updates; choose querySelectorAll when you need a fixed list at one moment in time.

🚀 Common Use Cases

  • Reading every <td> or <th> in a data table.
  • Collecting all <li> items inside a list component.
  • Iterating over <option> elements in a custom select widget.
  • Counting descendant nodes with the "*" wildcard.
  • Legacy scripts that rely on live HTMLCollections.
  • Teaching the difference between tag, class, and selector-based lookups.

🧠 How getElementsByTagName() Finds Matches

1

Pass a tag name

getElementsByTagName("td") or "*"

Args
2

Walk descendant subtree

Browser scans every descendant (not the root element itself).

Search
3

Return live HTMLCollection

Matching elements in document order; updates when DOM changes (MDN).

Result
4

Loop or modernize

Use for...of on the collection; for complex selectors use querySelectorAll.

📝 Notes

  • Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available, July 2015).
  • Returns a live HTMLCollection, not a static snapshot.
  • Use "*" to match all descendant elements (MDN).
  • In HTML documents, tag names are lower-cased before searching (MDN).
  • For camel-cased SVG tags in HTML, use getElementsByTagNameNS() (MDN).
  • Related: getElementsByClassName(), closest(), JavaScript hub.

Browser Support

Element.getElementsByTagName() is Baseline Widely available (MDN: across browsers since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline Widely available

Element.getElementsByTagName()

Find descendant elements by tag name with a live HTMLCollection — use * for every descendant.

Baseline Widely available
Google Chrome Supported · Desktop & Mobile
Yes
Microsoft Edge Supported · Chromium
Yes
Mozilla Firefox Supported · Desktop & Mobile
Yes
Apple Safari Supported · macOS & iOS
Yes
Opera Supported · Modern versions
Yes
Internet Explorer Supported in legacy IE
Yes
getElementsByTagName() Excellent

Bottom line: Use getElementsByTagName() for simple tag lookups in a subtree. Remember the collection is live and tag names are lower-cased in HTML documents.

Conclusion

Element.getElementsByTagName() returns a live HTMLCollection of descendant elements matching a tag name—a simple, fast way to query by tag within any element subtree.

Continue with getElementsByClassName(), closest(), shadowRoot, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use lowercase tag names in HTML documents (MDN)
  • Scope searches to a parent element when possible
  • Use "*" only when you truly need every descendant
  • Remember the collection is live after DOM mutations
  • Prefer querySelectorAll for complex CSS selectors

❌ Don’t

  • Assume the collection is static like querySelectorAll
  • Pass CSS selectors with angle brackets ("<div>")
  • Expect the caller element to be included in results
  • Use for camel-cased SVG tags in HTML without getElementsByTagNameNS
  • Confuse with getElementsByClassName (class vs tag)

Key Takeaways

Knowledge Unlocked

Five things to remember about Element.getElementsByTagName()

Live tag-based descendant search.

5
Core concepts
📄 02

Args

tag name

One
✍️ 03

Wildcard

"*"

All
04

Baseline

widely available

Status
05

Modern

querySelAll

Static

❓ Frequently Asked Questions

It returns a live HTMLCollection of descendant elements with the given tag name. The search is limited to the subtree rooted at the element you call it on, not the element itself.
No. MDN marks Element.getElementsByTagName() as Baseline Widely available (across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.
A string with the qualified tag name to look for. The special string '*' returns all descendant elements (MDN). In HTML documents, the argument is lower-cased before searching.
getElementsByTagName() returns a live HTMLCollection that updates when the DOM changes. querySelectorAll() returns a static NodeList snapshot.
Yes. MDN: the list updates automatically with the DOM tree, so you do not need to call the method again after DOM changes.
getElementsByTagName() matches by HTML tag name (like 'td' or 'p'). getElementsByClassName() matches by CSS class names.
Did you know?

MDN notes that the returned HTMLCollection is live and updates with the DOM tree automatically. You typically do not need to call getElementsByTagName() again after each change if you keep the same reference.

More Element Topics

Browse Element methods and properties to keep building your DOM skills.

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.

8 people found this page helpful