JavaScript Element getElementsByTagNameNS() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

In JavaScript, the getElementsByTagNameNS() method returns a live HTMLCollection of descendants that match a namespace URI and local name (MDN). Search is limited to the element’s subtree — not the element itself.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call el.getElementsByTagNameNS. Start with this line.

JavaScript
el.getElementsByTagNameNS(ns, localName)
CallWhat it does
el.getElementsByTagNameNS(ns, localName)Primary signature.
el.getElementsByTagNameNS(...)Call with the arguments your case needs.

Built into the browser. Call getElementsByTagNameNS(...) on an Element — no import required.

Library declarations
JavaScript
el.getElementsByTagNameNS(ns, localName)

Parameters

This member has no parameters beyond those shown in the signature.

Return Value

Return ValueDescription
*Live HTMLCollection

JavaScript Examples

Example 1 — SVG Circles

Find circle elements in the SVG namespace under a root.

JavaScript
const SVG = "http://www.w3.org/2000/svg";
const svg = document.createElementNS(SVG, "svg");
svg.appendChild(document.createElementNS(SVG, "circle"));
svg.appendChild(document.createElementNS(SVG, "circle"));

const circles = svg.getElementsByTagNameNS(SVG, "circle");
console.log(circles.length);
Try It Yourself

How It Works

  1. Use the SVG namespace URI. http://www.w3.org/2000/svg.
  2. Create nodes with createElementNS. Keeps the correct namespace.
  3. Search with the same URI + local name. Here: "circle".
  4. Read length. Only SVG circles under that root match.

Example 2 — XHTML Table Cells (MDN)

MDN-style search for td in the XHTML namespace.

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

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

How It Works

  1. Set the XHTML namespace. MDN: http://www.w3.org/1999/xhtml.
  2. Scope to a table. Only descendants of that table are searched.
  3. Match local name "td". Same idea as tag search, with a namespace.
  4. Loop and read attributes. Useful in XHTML / mixed documents.

Example 3 — Wildcard localName

Match every element in a namespace under the root.

JavaScript
const SVG = "http://www.w3.org/2000/svg";
const svg = document.createElementNS(SVG, "svg");
svg.appendChild(document.createElementNS(SVG, "circle"));
svg.appendChild(document.createElementNS(SVG, "rect"));

const all = svg.getElementsByTagNameNS(SVG, "*");
console.log(all.length);
Try It Yourself

How It Works

  1. Keep the namespace fixed. Still required as the first argument.
  2. Pass "*" as localName. MDN: matches all elements in that namespace.
  3. Count descendants. Both circle and rect are included.
  4. Root excluded. The svg container itself is not in the collection.

Browser Support

Element.getElementsByTagNameNS() is Baseline Widely available (MDN: across browsers since July 2015). Safe to use in modern evergreen browsers.

✓ Baseline · Widely available

getElementsByTagNameNS()

Live HTMLCollection of descendants matching a namespace URI and local name.

99% Widely available
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
getElementsByTagNameNS() Excellent

Bottom line: Use for SVG/XHTML/MathML lookups; use getElementsByTagName for ordinary HTML tags when namespaces do not matter.

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 of the element you call it on (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, or http://www.w3.org/2000/svg for SVG). localName — the local tag name, or "*" for all elements in that namespace (MDN).
Use getElementsByTagNameNS() for namespace-aware matching—especially SVG/MathML, or camel-cased SVG tags in HTML where getElementsByTagName() lower-cases the argument (MDN).
Yes. 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).

Next: getHTML()

Learn how to serialize an element’s HTML contents.

getHTML tutorial →

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.

11 people found this page helpful