Element.getElementsByTagNameNS

  • Revision slug: Web/API/element.getElementsByTagNameNS
  • Revision title: element.getElementsByTagNameNS
  • Revision id: 477009
  • Created:
  • Creator: kscarfone
  • Is current revision? Yes
  • Comment Updated tags

Revision Content

{{ DomRef() }} {{ fx_minversion_note("3.6", "See the Notes section below for changes to this API in Firefox 3.6.") }}

Summary

Returns a list of elements with the given tag name belonging to the given namespace.

Syntax

elements = element.getElementsByTagNameNS(namespaceURI, localName)
  • elements is a live {{ domxref("NodeList") }} (but see the note below) of found elements in the order they appear in the tree.
  • element is the element from where the search should start. Note that only the descendants of this element are included in the search, not the node itself.
  • namespaceURI is the namespace URI of elements to look for (see element.namespaceURI). For example, if you need to look for XHTML elements, use the XHTML namespace URI, http://www.w3.org/1999/xhtml.
  • localName is either the local name of elements to look for or the special value "*", which matches all elements (see element.localName).
Note: While the W3C specification says elements is a NodeList, this method returns a {{ domxref("HTMLCollection") }} both in Gecko and Internet Explorer. Opera returns a NodeList, but with a namedItem method implemented, which makes it similar to a HTMLCollection. As of January 2012, only in WebKit browsers is the returned value a pure NodeList. See bug 14869 for details.

Example

// check the alignment on a number of cells in a table in an XHTML document.
var table = document.getElementById("forecast-table");
var cells = table.getElementsByTagNameNS("http://www.w3.org/1999/xhtml", "td");

for (var i = 0; i < cells.length; i++) {
    var axis = cells[i].getAttribute("axis");
    if (axis == "year") {
        // grab the data
    }
}

Notes

element.getElementsByTagNameNS is similar to {{ Domxref("document.getElementsByTagNameNS") }}, except that its search is restricted to descendants of the specified element.

The behavior of element.getElementsByTagNameNS changed between Firefox 3.5 and Firefox 3.6.

  • In Firefox 3.5 and before, this function would automatically case-fold any queries so that a search for "foo" would match "Foo" or "foo".
  • In Firefox 3.6 and later this function is now case-sensitive so that a query for "foo" will only match "foo" and not "Foo".

For more background on this, please see this comment from Henri Sivonen about the change. You can also look at the relevant part of the standard, which states which parts of the API are case-sensitive and which parts aren't.

Specification

Revision Source

<div>
  {{ DomRef() }} {{ fx_minversion_note("3.6", "See the Notes section below for changes to this API in Firefox 3.6.") }}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>Returns a list of elements with the given tag name belonging to the given namespace.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<em>elements</em> = <em>element</em>.getElementsByTagNameNS(<em>namespaceURI</em>, <em>localName</em>)</pre>
<ul>
  <li><code>elements</code> is a live {{ domxref("NodeList") }} (but see the note below) of found elements in the order they appear in the tree.</li>
  <li><code>element</code> is the element from where the search should start. Note that only the descendants of this element are included in the search, not the node itself.</li>
  <li><code>namespaceURI</code> is the namespace URI of elements to look for (see <a href="/en-US/docs/DOM/Node.namespaceURI" title="DOM/Node.namespaceURI"><code>element.namespaceURI</code></a>). For example, if you need to look for XHTML elements, use the XHTML namespace URI, <span class="nowiki"><code>http://www.w3.org/1999/xhtml</code></span>.</li>
  <li><code>localName</code> is either the local name of elements to look for or the special value <code>"*"</code>, which matches all elements (see <a href="/en-US/docs/DOM/Node.localName" title="DOM/Node.localName"><code>element.localName</code></a>).</li>
</ul>
<div class="note">
  <strong>Note:</strong> While the W3C specification says <code>elements</code> is a <code>NodeList</code>, this method returns a {{ domxref("HTMLCollection") }} both in Gecko and Internet Explorer. Opera returns a <code>NodeList</code>, but with a <code>namedItem</code> method implemented, which makes it similar to a <code>HTMLCollection</code>. As of January 2012, only in WebKit browsers is the returned value a pure <code>NodeList</code>. See <a class="link-https" href="https://bugzilla.mozilla.org/show_bug.cgi?id=14869" title="https://bugzilla.mozilla.org/show_bug.cgi?id=14869">bug 14869</a> for details.</div>
<h2 id="Example" name="Example">Example</h2>
<pre class="brush:js">
// check the alignment on a number of cells in a table in an XHTML document.
var table = document.getElementById("forecast-table");
var cells = table.getElementsByTagNameNS("http://www.w3.org/1999/xhtml", "td");

for (var i = 0; i &lt; cells.length; i++) {
    var axis = cells[i].getAttribute("axis");
    if (axis == "year") {
        // grab the data
    }
}</pre>
<h2 id="Notes" name="Notes">Notes</h2>
<p><code>element.getElementsByTagNameNS</code> is similar to {{ Domxref("document.getElementsByTagNameNS") }}, except that its search is restricted to descendants of the specified element.</p>
<p>The behavior of <code>element.getElementsByTagNameNS</code> changed between Firefox 3.5 and Firefox 3.6.</p>
<ul>
  <li>In Firefox 3.5 and before, this function would automatically case-fold any queries so that a search for "foo" would match "Foo" or "foo".</li>
  <li>In Firefox 3.6 and later this function is now case-sensitive so that a query for "foo" will only match "foo" and not "Foo".</li>
</ul>
<p>For more background on this, please see this <a class="link-https" href="https://bugzilla.mozilla.org/show_bug.cgi?id=542185#c5" title="https://bugzilla.mozilla.org/show_bug.cgi?id=542185#c5">comment from Henri Sivonen about the change</a>. You can also look at the <a href="/en-US/docs/Case_Sensitivity_in_class_and_id_Names" title="Case Sensitivity in class and id Names">relevant part of the standard, which states which parts of the API are case-sensitive and which parts aren't</a>.</p>
<h2 id="Specification" name="Specification">Specification</h2>
<ul>
  <li><a class="external" href="http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-A6C90942">DOM Level 2 Core: Element.getElementsByTagNameNS</a></li>
</ul>
Revert to this revision