JavaScript Document getElementsByTagNameNS() Method

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

What You’ll Learn

document.getElementsByTagNameNS() is an instance method that returns elements matching a namespace URI and a local tag name (see MDN Document: getElementsByTagNameNS()). Learn when to use it for SVG/HTML namespaces, how it differs from getElementsByTagName(), and five try-it labs.

01

Kind

Instance method

02

Args

namespace + name

03

Returns

HTMLCollection

04

Live?

Yes

05

Case

Sensitive

06

Status

Baseline

Introduction

Regular HTML pages usually need only getElementsByTagName(). When SVG (or other namespaces) mix into the document, tag names alone may not be enough — you also need the namespace URI.

MDN: getElementsByTagNameNS returns a list of elements with the given tag name belonging to the given namespace. The complete document is searched, including the root node.

💡
Think: tag name + “which vocabulary?”

1) Pick a namespace URI (HTML or SVG)
2) Pick a local name like "p" or "circle"
3) Call document.getElementsByTagNameNS(namespace, name)
4) Get a live HTMLCollection in tree order

📄
Common namespace URIs

HTML/XHTML: http://www.w3.org/1999/xhtml
SVG: http://www.w3.org/2000/svg
These strings are identifiers — they do not require a network request.

Related tutorials: getElementsByTagName(), getElementsByClassName(), getSelection().

Understanding document.getElementsByTagNameNS()

An instance method on the Document interface (MDN). The same API also exists on elements for scoped searches.

  • namespace — the namespace URI of elements to look for (see element.namespaceURI) (MDN).
  • name — local name to match, or * for all elements in that namespace (see element.localName) (MDN).
  • Case-sensitive — unlike getElementsByTagName(), parameters are case-sensitive (MDN).
  • Return value — a live HTMLCollection in tree order (MDN).
  • Whole document — search includes the root node (MDN).
  • Element scope — call on a parent to search only descendants (MDN).

📝 Syntax

General form of Document.getElementsByTagNameNS (MDN):

JavaScript
getElementsByTagNameNS(namespace, name)

Parameters

  • namespace — the namespace URI of elements to look for (MDN).
  • name — either the local name of elements to look for or the special value *, which matches all elements (MDN).

Return value

A live HTMLCollection of found elements in the order they appear in the tree (MDN).

MDN note on case

Unlike document.getElementsByTagName(), the parameters for getElementsByTagNameNS() are case-sensitive (MDN).

MDN quick sample

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

⚡ Quick Reference

GoalCode
XHTML paragraphsdocument.getElementsByTagNameNS("http://www.w3.org/1999/xhtml", "p")
SVG circlesdocument.getElementsByTagNameNS("http://www.w3.org/2000/svg", "circle")
All in a namespacedocument.getElementsByTagNameNS(SVG_NS, "*")
Inside a parentel.getElementsByTagNameNS(XHTML_NS, "p")
First matchdocument.getElementsByTagNameNS(SVG_NS, "path")[0]
MDN statusBaseline Widely available (since Jul 2015)

🔍 At a Glance

Four facts about document.getElementsByTagNameNS().

Returns
HTMLCollection

live

Args
ns + name

URI + local

Case
sensitive

MDN

Status
Baseline

since 2015

📋 HTML namespace vs SVG namespace

HTML / XHTMLSVG
Namespace URIhttp://www.w3.org/1999/xhtmlhttp://www.w3.org/2000/svg
Example local namesp, div, spancircle, path, text
Why NS helpsExplicit HTML vocabularyAvoids HTML lowercasing pitfalls for SVG tags
Beginner tipOften getElementsByTagName is enoughPrefer NS when targeting SVG precisely

Examples Gallery

Examples follow MDN Document: getElementsByTagNameNS() and practical SVG/HTML patterns.

📚 Getting Started

Look up tags with an explicit namespace URI.

Example 1 — MDN: count XHTML <p> elements

Same idea as MDN’s document-wide paragraph count, with the XHTML namespace.

JavaScript
const XHTML_NS = "http://www.w3.org/1999/xhtml";
const allParas = document.getElementsByTagNameNS(XHTML_NS, "p");
console.log(`There are ${allParas.length} <p> elements in this document`);
Try It Yourself

How It Works

In a normal HTML page, paragraph elements live in the XHTML namespace. Passing that URI makes the lookup explicit (MDN).

Example 2 — Search inside a parent

MDN: call getElementsByTagNameNS on an element to limit the search.

JavaScript
const XHTML_NS = "http://www.w3.org/1999/xhtml";
const div1 = document.getElementById("div1");
const div1Paras = div1.getElementsByTagNameNS(XHTML_NS, "p");
console.log(`There are ${div1Paras.length} <p> elements in div1 element`);
Try It Yourself

How It Works

Nested paragraphs still count: descendants inside inner boxes are included when you search from an outer parent (same idea as MDN’s nested demo).

📈 Practical Patterns

SVG lookups, wildcards, and side-by-side comparisons.

Example 3 — Find SVG <circle> elements

Use the SVG namespace URI for graphics markup.

JavaScript
const SVG_NS = "http://www.w3.org/2000/svg";
const circles = document.getElementsByTagNameNS(SVG_NS, "circle");
console.log(circles.length);
if (circles[0]) {
  console.log(circles[0].getAttribute("r"));
}
Try It Yourself

How It Works

SVG elements are not in the HTML namespace. Passing the SVG URI selects only those graphics nodes.

Example 4 — Wildcard * in a namespace

MDN: * matches all elements for the given namespace.

JavaScript
const SVG_NS = "http://www.w3.org/2000/svg";
const svgNodes = document.getElementsByTagNameNS(SVG_NS, "*");
console.log(svgNodes.length);
for (const el of svgNodes) {
  console.log(el.localName);
}
Try It Yourself

How It Works

* still respects the namespace filter — you get every SVG element, not every HTML element on the page.

Example 5 — NS lookup vs plain tag lookup

See how both APIs can find HTML paragraphs, while SVG prefers NS.

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

console.log("plain p:", document.getElementsByTagName("p").length);
console.log("NS p:", document.getElementsByTagNameNS(XHTML_NS, "p").length);
console.log("NS circle:", document.getElementsByTagNameNS(SVG_NS, "circle").length);
Try It Yourself

How It Works

For everyday HTML, both counts often match. Reach for NS when namespaces matter — especially SVG.

🚀 Common Use Cases

  • SVG tooling — select circle, path, or text nodes precisely.
  • Mixed documents — pages that embed SVG inside HTML.
  • Case-sensitive names — avoid HTML lowercasing surprises (MDN).
  • Scoped widgets — search only inside one container element.
  • Namespace audits — list every node in the SVG namespace with *.
  • Prefer plain tag lookup — for simple HTML-only pages, use getElementsByTagName().

🧠 How getElementsByTagNameNS() Works

1

Pass a namespace URI

Matches element.namespaceURI (MDN).

Namespace
2

Pass a local name (or *)

Matches element.localName, or all tags with * (MDN).

Name
3

Search document or subtree

Whole document on document; descendants on an element (MDN).

Scope
4

Live HTMLCollection

Tree-ordered matches that stay synced with the DOM.

📝 Notes

  • MDN: Baseline Widely available since July 2015.
  • MDN: returns a live HTMLCollection in tree order.
  • MDN: parameters are case-sensitive (unlike getElementsByTagName).
  • MDN: * matches all elements for the given namespace.
  • Namespace URIs are identifiers, not pages you must download.
  • Related: getElementsByTagName(), getElementsByClassName(), getSelection().

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 of elements matching a 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: Use getElementsByTagNameNS for SVG and mixed namespaces. Prefer getElementsByTagName for everyday HTML-only tag lists.

Conclusion

document.getElementsByTagNameNS(namespace, name) finds tags in a specific namespace and returns a live HTMLCollection. Reach for it when SVG or other namespaces matter; keep using plain getElementsByTagName for simple HTML.

Continue with getElementsByTagName(), getSelection(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Store namespace URIs in named constants (SVG_NS, XHTML_NS)
  • Use NS lookups for SVG and mixed documents
  • Remember parameters are case-sensitive (MDN)
  • Scope searches with an element root when possible
  • Use * carefully to audit one namespace

❌ Don’t

  • Treat the namespace URI as a download URL
  • Assume HTML lowercasing rules apply here (MDN)
  • Overcomplicate simple HTML with NS when plain tag lookup is enough
  • Forget the collection is live and ordered by the tree (MDN)
  • Skip checking length before using [0]

Key Takeaways

Knowledge Unlocked

Five things to remember about getElementsByTagNameNS()

Namespace-aware live tag matches.

5
Core concepts
🔄02

Args

URI + name

NS
🎯03

Case

sensitive

MDN
04

Best for

SVG / mixed

namespaces
🛡05

Status

Baseline

2015

❓ 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().
When you work with SVG or mixed namespaces and need an exact local name (for example camel-case SVG tags that getElementsByTagName may lower-case in HTML).
Yes. MDN: when the node is not the document, Element.getElementsByTagNameNS() is used to search that subtree.
Did you know?

MDN’s classic demo for this method is designed to be saved as an .xhtml file — namespaces are clearer when the document itself is XML/XHTML flavored.

Next: getSelection()

Learn how to read the user’s highlighted text and caret with the 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.

6 people found this page helpful