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
Fundamentals
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 liveHTMLCollection. 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).
Concept
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 liveHTMLCollection 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).
Foundation
📝 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]);
Cheat Sheet
⚡ Quick Reference
Goal
Code
Find by tag
el.getElementsByTagName("p")
All descendants
el.getElementsByTagName("*")
Table cells
table.getElementsByTagName("td")
Count matches
collection.length
Static alternative
el.querySelectorAll("p")
MDN status
Baseline Widely available (since July 2015)
Snapshot
🔍 At a Glance
Four facts to remember about Element.getElementsByTagName().
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
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.
BaselineWidely available
Google ChromeSupported · Desktop & Mobile
Yes
Microsoft EdgeSupported · Chromium
Yes
Mozilla FirefoxSupported · Desktop & Mobile
Yes
Apple SafariSupported · macOS & iOS
Yes
OperaSupported · Modern versions
Yes
Internet ExplorerSupported 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.
Wrap Up
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.
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)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Element.getElementsByTagName()
Live tag-based descendant search.
5
Core concepts
📝01
Returns
HTMLCollection
API
📄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.