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
Fundamentals
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.
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`);
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.
BaselineWidely available
Google ChromeSupported
Yes
Mozilla FirefoxSupported
Yes
Apple SafariSupported
Yes
Microsoft EdgeSupported
Yes
OperaSupported
Yes
Internet ExplorerSupported (legacy)
Yes
getElementsByTagNameNS()Wide
Bottom line: Use getElementsByTagNameNS for SVG and mixed namespaces. Prefer getElementsByTagName for everyday HTML-only tag lists.
Wrap Up
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.
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]
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about getElementsByTagNameNS()
Namespace-aware live tag matches.
5
Core concepts
📝01
Returns
HTMLCollection
MDN
🔄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.