JavaScript Document getElementsByTagNameNS() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

document.getElementsByTagNameNS(namespace, name) returns a live HTMLCollection of elements that match both a namespace URI and a local name (MDN). Unlike getElementsByTagName(), both parameters are case-sensitive.

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

Syntax

Call document.getElementsByTagNameNS. Start with this line.

JavaScript
document.getElementsByTagNameNS(namespace, name)
CallWhat it does
document.getElementsByTagNameNS(namespace, name)Primary signature.
document.getElementsByTagNameNS(...)Call with the arguments your case needs.

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

Library declarations
JavaScript
document.getElementsByTagNameNS(namespace, name)

Parameters

ParameterDescription
namespaceRequired. Namespace URI to match (see element.namespaceURI)
nameRequired. Local name, or * for all in that namespace

Return Value

Return ValueDescription
*Live HTMLCollection in tree order

JavaScript Examples

Example 1 — Count XHTML Paragraphs

MDN-style lookup: namespace URI for XHTML plus local name "p".

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

console.log(allParas.length);
console.log(allParas[0].localName); // "p"
Try It Yourself

How It Works

  1. Set the namespace. HTML elements use the XHTML namespace URI (MDN example).
  2. Pass the local name. "p" matches paragraph elements in that namespace.
  3. Get a live list. Result is an HTMLCollection in tree order (MDN).
  4. Read safely. Use .length and .localName on matches.

Example 2 — Find SVG Circles

Use the SVG namespace so you only collect SVG shapes, not HTML tags with the same name.

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

console.log(circles.length);           // e.g. 2
console.log(circles[0].namespaceURI);  // SVG_NS
Try It Yourself

How It Works

  1. Use the SVG URI. Only nodes in http://www.w3.org/2000/svg match.
  2. Match circle. Local name must match exactly (case-sensitive).
  3. Ignore HTML. HTML elements are in a different namespace, so they stay out.
  4. Confirm. namespaceURI on a hit should equal the SVG constant.

Example 3 — Case Sensitivity

MDN: unlike getElementsByTagName(), NS parameters are case-sensitive.

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

const lower = document.getElementsByTagNameNS(XHTML_NS, "p");
const upper = document.getElementsByTagNameNS(XHTML_NS, "P");

console.log(lower.length); // > 0 when paragraphs exist
console.log(upper.length); // 0 — wrong case
Try It Yourself

How It Works

  1. Correct local name. HTML paragraphs use lowercase p.
  2. Wrong case fails. "P" does not match (MDN case-sensitive rule).
  3. Contrast. Plain getElementsByTagName lower-cases in HTML documents.
  4. Rule. Copy namespace URIs and local names carefully — typos yield empty lists.

Browser Support

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

Live HTMLCollection by namespace URI and local 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
getElementsByTagNameNS() Wide

Bottom line: Safe for SVG and mixed-namespace lookups. Prefer getElementsByTagName for simple HTML tags. See the MDN Document.getElementsByTagNameNS() reference.

Frequently Asked Questions

MDN: it returns a list of elements with the given tag name belonging to the given namespace. The complete document is searched, including the root node.
No. MDN marks Document.getElementsByTagNameNS() 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).
You must pass a namespace URI plus a local name. MDN also notes the parameters are case-sensitive, unlike document.getElementsByTagName().
MDN: the special value * matches all elements in that namespace (see element.localName).
Yes. MDN: when the node is not the document, Element.getElementsByTagNameNS() is used to search that subtree.

Next: getSelection()

Learn how document.getSelection() returns the current Selection object.

getSelection() →

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.

12 people found this page helpful