JavaScript Element getElementsByTagNameNS() Method
Beginner
⏱️ 13 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline
Instance method
Overview
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
Fundamentals
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 liveHTMLCollection. 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).
Concept
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 liveHTMLCollection 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).
Foundation
📝 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).
MDN recommends getElementsByTagNameNS() for camel-cased SVG elements in HTML documents. This is a key reason the namespace variant still matters today.
Applications
🚀 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.
Important
📝 Notes
Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available, July 2015).
Returns a liveHTMLCollection, 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).
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.
BaselineWidely available
Google ChromeSupported · Desktop & Mobile
Yes
Microsoft EdgeSupported · Chromium
Yes
Mozilla FirefoxSupported · Desktop & Mobile
Yes
Apple SafariSupported · macOS & iOS
Yes
OperaSupported · Modern versions
Yes
Internet ExplorerSupported 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.
Wrap Up
Conclusion
Element.getElementsByTagNameNS() returns a live HTMLCollection of descendant elements matching a namespace URI and local name—the namespace-aware companion to getElementsByTagName().
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)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Element.getElementsByTagNameNS()
Namespace-aware descendant search.
5
Core concepts
📝01
Returns
HTMLCollection
API
📄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.