Element.getAttributeNS

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

Revision Content

{{ DomRef() }}

Summary

getAttributeNS returns the string value of the attribute with the specified namespace and name. If the named attribute does not exist, the value returned will either be null or "" (the empty string); see {{ Anch("Notes") }} for details.

Syntax

attrVal = element.getAttributeNS(namespace, name)

Parameters

namespace
The namespace in which to look for the specified attribute.
name
The name of the attribute to look for.

Return value

The string value of the specified attribute. If the attribute doesn't exist, the result is null.

Note: Earlier versions of the DOM specification had this method described as returning an empty string for non-existent attributes, but it was not typically implemented this way since null makes more sense. The DOM4 specification now says this method should return null for non-existent attributes.

Example

var div1 = document.getElementById("div1");
var a = div1.getAttributeNS("www.mozilla.org/ns/specialspace/", 
                            "special-align");
alert(a); // shows the value of align for that div

Notes

getAttributeNS() differs from {{ domxref("element.getAttribute()", "getAttribute()") }} in that it allows you to further specify the requested attribute as being part of a particular namespace, as in the example above, where the attribute is part of the fictional "specialspace" namespace on Mozilla.

Prior to the DOM4 specification, this method was specified to return an empty string rather than null for non-existent attributes. However, most browsers instead returned null. Starting with DOM4, the specification now says to return null. However, some older browsers return an empty string. For that reason, you should use {{ domxref("element.hasAttributeNS()", "hasAttributeNS()") }} to check for an attribute's existence prior to calling getAttributeNS() if it is possible that the requested attribute does not exist on the specified element.

{{ DOMAttributeMethods() }}

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}

Gecko notes

Starting in Gecko 13.0 {{ geckoRelease("13.0") }}, null is always returned instead of the empty string, as per the DOM4 specification. Previously, there were cases in which an empty string could be returned.

Specification

See also

 

{{ languages( { "fr": "fr/DOM/element.getAttributeNS", "ja": "ja/DOM/element.getAttributeNS", "pl": "pl/DOM/element.getAttributeNS" } ) }}

Revision Source

<p>{{ DomRef() }}</p>
<h3 id="Summary" name="Summary">Summary</h3>
<p><code>getAttributeNS</code> returns the string value of the attribute with the specified namespace and name. If the named attribute does not exist, the value returned will either be <code>null</code> or <code>""</code> (the empty string); see {{ Anch("Notes") }} for details.</p>
<h3 id="Syntax" name="Syntax">Syntax</h3>
<pre class="eval">
<em>attrVal</em> = <em>element</em>.getAttributeNS(<em>namespace</em>, <em>name</em>)
</pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<dl>
  <dt>
    <code>namespace</code></dt>
  <dd>
    The namespace in which to look for the specified attribute.</dd>
  <dt>
    <code>name</code></dt>
  <dd>
    The name of the attribute to look for.</dd>
</dl>
<h3 id="Return_value">Return value</h3>
<p>The string value of the specified attribute. If the attribute doesn't exist, the result is <code>null</code>.</p>
<div class="note">
  <strong>Note:</strong> Earlier versions of the DOM specification had this method described as returning an empty string for non-existent attributes, but it was not typically implemented this way since null makes more sense. The DOM4 specification now says this method should return null for non-existent attributes.</div>
<h3 id="Example" name="Example">Example</h3>
<pre class="eval">
var div1 = document.getElementById("div1");
var a = div1.getAttributeNS("www.mozilla.org/ns/specialspace/", 
                            "special-align");
alert(a); // shows the value of align for that div
</pre>
<h3 id="Notes" name="Notes">Notes</h3>
<p><code>getAttributeNS()</code> differs from {{ domxref("element.getAttribute()", "getAttribute()") }} in that it allows you to further specify the requested attribute as being part of a particular namespace, as in the example above, where the attribute is part of the fictional "specialspace" namespace on Mozilla.</p>
<p>Prior to the DOM4 specification, this method was specified to return an empty string rather than null for non-existent attributes. However, most browsers instead returned null. Starting with DOM4, the specification now says to return null. However, some older browsers return an empty string. For that reason, you should use {{ domxref("element.hasAttributeNS()", "hasAttributeNS()") }} to check for an attribute's existence prior to calling <code>getAttributeNS()</code> if it is possible that the requested attribute does not exist on the specified element.</p>
<p>{{ DOMAttributeMethods() }}</p>
<h3 id="Browser_compatibility">Browser compatibility</h3>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Chrome</th>
        <th>Firefox (Gecko)</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th>Safari</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Android</th>
        <th>Chrome for Android</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>IE Mobile</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
      </tr>
    </tbody>
  </table>
</div>
<h4 id="Gecko_notes">Gecko notes</h4>
<p>Starting in Gecko 13.0 {{ geckoRelease("13.0") }}, <code>null</code> is always returned instead of the empty string, as per the DOM4 specification. Previously, there were cases in which an empty string could be returned.</p>
<h3 id="Specification">Specification</h3>
<ul>
  <li><a class="external" href="http://www.w3.org/TR/dom/#dom-element-getattributens" title="http://www.w3.org/TR/dom/#dom-element-getattributens">DOM4: getAttributeNS()</a></li>
  <li>
    <p><a class="external" href="http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-ElGetAttrNS">DOM Level 2 Core: getAttributeNS</a></p>
  </li>
</ul>
<h3 id="See_also" name="See_also">See also</h3>
<ul>
  <li><a href="/en/Code_snippets/getAttributeNS" title="en/Code_snippets/getAttributeNS">Code snippets:getAttributeNS</a></li>
</ul>
<p>&nbsp;</p>
<p>{{ languages( { "fr": "fr/DOM/element.getAttributeNS", "ja": "ja/DOM/element.getAttributeNS", "pl": "pl/DOM/element.getAttributeNS" } ) }}</p>
Revert to this revision