JavaScript Element getElementsByTagNameNS() Method

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

What You’ll Learn

Element.getElementsByTagNameNS() is an instance method that finds descendant elements by namespace URI and local name, returning a live HTMLCollection. Learn the XHTML and SVG namespaces, MDN’s table-cell pattern, the "*" wildcard, when to prefer this over getElementsByTagName(), and five try-it labs aligned with MDN.

01

Kind

Instance method

02

Returns

HTMLCollection

03

Args

URI + localName

04

Scope

Descendants

05

Wildcard

"*" localName

06

Status

Baseline widely

Introduction

Most everyday HTML uses one namespace — the XHTML namespace (http://www.w3.org/1999/xhtml). When you embed SVG or MathML, elements belong to different namespaces. getElementsByTagNameNS() lets you search by both namespace URI and local name instead of a plain tag string.

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, ordinary elements like <p> and <td> use the XHTML namespace URI. For camel-cased SVG tags in HTML (like linearGradient), MDN recommends getElementsByTagNameNS() over getElementsByTagName(). For simple HTML-only pages, querySelectorAll() is often easier.

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

Understanding the getElementsByTagNameNS() Method

Calling el.getElementsByTagNameNS(namespaceURI, localName) walks the subtree under el and collects every descendant whose namespace and local name match.

  • It is an instance method on Element.
  • namespaceURI — the namespace of elements to find (see Element.namespaceURI, MDN).
  • localName — the local tag name, or "*" for all in that namespace (MDN).
  • Returns a live HTMLCollection in document order.
  • Searches descendants only—not the element itself.
  • Common URI: http://www.w3.org/1999/xhtml for HTML/XHTML elements.
  • Also available on Document for whole-page searches (MDN).

📝 Syntax

General form of Element.getElementsByTagNameNS (MDN):

JavaScript
getElementsByTagNameNS(namespaceURI, localName)

Parameters

namespaceURI — the namespace URI of elements to look for. For XHTML/HTML elements, use http://www.w3.org/1999/xhtml (MDN). For SVG, use http://www.w3.org/2000/svg.

localName — the local name of elements to look for, or the special value "*" which matches all elements in that namespace (MDN).

Return value

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

Common patterns

JavaScript
const XHTML_NS = "http://www.w3.org/1999/xhtml";
const SVG_NS = "http://www.w3.org/2000/svg";

// XHTML paragraphs in a section
const paragraphs = section.getElementsByTagNameNS(XHTML_NS, "p");

// MDN table-cell pattern
const cells = table.getElementsByTagNameNS(XHTML_NS, "td");

// SVG circles inside an inline SVG root
const circles = svgRoot.getElementsByTagNameNS(SVG_NS, "circle");

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

⚡ Quick Reference

GoalCode
Find by namespaceel.getElementsByTagNameNS(XHTML_NS, "p")
XHTML namespace"http://www.w3.org/1999/xhtml"
SVG namespace"http://www.w3.org/2000/svg"
Table cells (MDN)table.getElementsByTagNameNS(XHTML_NS, "td")
All in namespaceel.getElementsByTagNameNS(XHTML_NS, "*")
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about Element.getElementsByTagNameNS().

Returns
HTMLCollection

Live list

Baseline
widely

Since Jul 2015

XHTML URI
1999/xhtml

HTML elements

Wildcard
"*"

localName

📋 Namespace-aware DOM APIs

getElementsByTagNameNS()getElementsByTagName()querySelectorAll()
ReturnsLive HTMLCollectionLive HTMLCollectionStatic NodeList
Matches byNamespace URI + local nameTag name stringCSS selector
SVG in HTMLCamel-case local names workMay fail for camel-case SVGWorks with selectors
Wildcard"*" localName"*" tag nameFull selector syntax
Best forXML/SVG/MathML namespacesSimple HTML tag lookupsComplex selectors

Examples Gallery

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

📚 Getting Started

MDN: namespace-aware table cells and XHTML paragraphs.

Example 1 — Table <td> with axis

MDN forecast-table pattern: find XHTML cells and filter by axis.

JavaScript
const XHTML_NS = "http://www.w3.org/1999/xhtml";
const table = document.getElementById("forecast-table");
const cells = table.getElementsByTagNameNS(XHTML_NS, "td");

for (const cell of cells) {
  if (cell.getAttribute("axis") === "year") {
    console.log(cell.textContent);
  }
}
Try It Yourself

How It Works

HTML table cells live in the XHTML namespace. Passing that URI plus "td" matches every cell descendant, then you filter by attribute (MDN).

Example 2 — XHTML <p> in a Section

Namespace-aware paragraph search inside a container.

JavaScript
const XHTML_NS = "http://www.w3.org/1999/xhtml";
const article = document.getElementById("article");
const paragraphs = article.getElementsByTagNameNS(XHTML_NS, "p");

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

How It Works

In HTML documents, ordinary <p> elements use the XHTML namespace URI. Paragraphs outside #article are ignored.

📈 Practical Patterns

SVG namespaces, wildcards, and comparison with plain tag search.

Example 3 — SVG <circle> Elements

Find shapes inside an inline SVG using the SVG namespace URI.

JavaScript
const SVG_NS = "http://www.w3.org/2000/svg";
const svgRoot = document.getElementById("chart");
const circles = svgRoot.getElementsByTagNameNS(SVG_NS, "circle");

console.log(circles.length);
console.log(circles[0].getAttribute("r"));
Try It Yourself

How It Works

SVG elements belong to http://www.w3.org/2000/svg. Namespace-aware search is the reliable way to target them inside mixed HTML+SVG documents.

Example 4 — Wildcard localName

MDN: "*" as localName matches every element in that namespace.

JavaScript
const XHTML_NS = "http://www.w3.org/1999/xhtml";
const container = document.getElementById("widget");
const xhtmlNodes = container.getElementsByTagNameNS(XHTML_NS, "*");

console.log(xhtmlNodes.length);
// Every XHTML-namespace descendant inside #widget
Try It Yourself

How It Works

The wildcard applies to the local name, not the namespace. SVG elements in the same container are excluded because they use a different URI.

Example 5 — vs getElementsByTagName()

Camel-cased SVG tags in HTML: namespace method wins (MDN).

JavaScript
const SVG_NS = "http://www.w3.org/2000/svg";
const svg = document.getElementById("gradient-svg");

const byNS = svg.getElementsByTagNameNS(SVG_NS, "linearGradient");
const byTag = svg.getElementsByTagName("linearGradient");

console.log(byNS.length);  // finds camel-case SVG
console.log(byTag.length); // often 0 in HTML
Try It Yourself

How It Works

MDN recommends getElementsByTagNameNS() for camel-cased SVG elements in HTML documents. This is a key reason the namespace variant still matters today.

🚀 Common Use Cases

  • Reading XHTML table cells in mixed or XML-style documents (MDN).
  • Collecting SVG shapes like <circle> or <path> inside inline SVG.
  • Finding camel-cased SVG elements such as linearGradient in HTML.
  • Counting all descendants in one namespace with localName "*".
  • Legacy XML tooling that relies on namespace-aware DOM walks.
  • Teaching the difference between plain tag search and namespace URIs.

🧠 How getElementsByTagNameNS() Finds Matches

1

Pass namespace + local name

getElementsByTagNameNS(XHTML_NS, "td") or SVG URI

Args
2

Walk descendant subtree

Browser scans descendants and compares namespace URI and local name.

Search
3

Return live HTMLCollection

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

Result
4

Loop or compare APIs

Use for...of on the collection; for simple HTML-only pages, querySelectorAll may be easier.

📝 Notes

  • Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available, July 2015).
  • Returns a live HTMLCollection, not a static snapshot.
  • Use "*" as localName to match all elements in a namespace (MDN).
  • HTML elements use namespace http://www.w3.org/1999/xhtml (MDN).
  • For camel-cased SVG in HTML, prefer this over getElementsByTagName() (MDN).
  • Related: getElementsByTagName(), getAttributeNS(), JavaScript hub.

Browser Support

Element.getElementsByTagNameNS() 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.getElementsByTagNameNS()

Find descendant elements by namespace URI and local name — essential for SVG, MathML, and XHTML-aware DOM walks.

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
getElementsByTagNameNS() Excellent

Bottom line: Use getElementsByTagNameNS() when namespace matters — especially SVG in HTML. For plain HTML-only selectors, querySelectorAll() is often simpler.

Conclusion

Element.getElementsByTagNameNS() returns a live HTMLCollection of descendant elements matching a namespace URI and local name—the namespace-aware companion to getElementsByTagName().

Continue with getElementsByTagName(), getAttributeNS(), shadowRoot, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Store namespace URIs in constants (XHTML_NS, SVG_NS)
  • Use for SVG/MathML or mixed-namespace documents
  • Use "*" localName only when you need every match in one namespace
  • Remember the collection is live after DOM mutations
  • Prefer querySelectorAll for simple HTML-only CSS selectors

❌ Don’t

  • Confuse namespace URI with a tag prefix (svg:circle)
  • Use plain getElementsByTagName for camel-case SVG in HTML
  • Expect the caller element to be included in results
  • Assume "*" matches every namespace at once
  • Hard-code wrong URIs (always verify with element.namespaceURI)

Key Takeaways

Knowledge Unlocked

Five things to remember about Element.getElementsByTagNameNS()

Namespace-aware descendant search.

5
Core concepts
📄 02

Args

URI + name

Two
✍️ 03

XHTML

1999/xhtml

HTML
04

SVG

2000/svg

Shapes
05

vs Tag

camelCase

SVG

❓ Frequently Asked Questions

It returns a live HTMLCollection of descendant elements that match a namespace URI and local name. The search is limited to the subtree rooted at the element you call it on, not the element itself (MDN).
No. MDN marks Element.getElementsByTagNameNS() as Baseline Widely available (across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.
namespaceURI — the namespace of elements to find (for example http://www.w3.org/1999/xhtml for XHTML). localName — the local tag name to match, or '*' for all elements in that namespace (MDN).
Use getElementsByTagNameNS() when you need namespace-aware matching — especially for SVG or MathML, or camel-cased SVG tags in HTML documents where getElementsByTagName() may not match (MDN).
Yes. MDN: the HTMLCollection updates automatically with the DOM tree, so you usually do not need to call the method again after each change.
Yes. Document.getElementsByTagNameNS() works the same way from the document root; Element.getElementsByTagNameNS() limits the search to one element's descendants (MDN).
Did you know?

MDN recommends getElementsByTagNameNS() when you need camel-cased SVG elements in HTML documents—something plain getElementsByTagName() may not match reliably.

More Element Topics

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

getHTML() →

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