Element.setAttributeNodeNS

  • Revision slug: DOM/element.setAttributeNodeNS
  • Revision title: element.setAttributeNodeNS
  • Revision id: 135362
  • Created:
  • Creator: Brettz9
  • Is current revision? No
  • Comment 4 words added, 6 words removed

Revision Content

{{ DomRef() }}

Summary

setAttributeNodeNS adds a new namespaced attribute node to an element.

Syntax

replacedAttr = element.setAttributeNodeNS(attributeNode) 
  • replacedAttr is the replaced attribute node, if any, returned by this function.
  • attributeNode is an Attr node.

Example

// <div id="one" xmlns:myNS="http://www.mozilla.org/ns/specialspace" 
            myNS:special-align="utterleft">one</div> 
// <div id="two">two</div> 


var myns = "http://www.mozilla.org/ns/specialspace"; 
var d1 = document.getElementById("one"); 
var d2 = document.getElementById("two"); 
var a = d1.getAttributeNodeNS(myns, "special-align"); 
d2.setAttributeNodeNS(a.cloneNode(true));
alert(d2.attributes[1].value) // returns: `utterleft'

Notes

If the specified attribute already exists on the element, then that attribute is replaced with the new one and the replaced one is returned.

Note that if you try to set without cloning the node, Mozilla gives an NS_ERROR_DOM_INUSE_ATTRIBUTE_ERR "Attribute already in use" error, as the DOM requires cloning for Attr to be reused (unlike other Nodes which can be moved).

{{ DOMAttributeMethods() }}

Specification

DOM Level 2 Core: setAttributeNodeNS

 

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

Revision Source

<p>{{ DomRef() }}</p>
<h3 id="Summary" name="Summary">Summary</h3>
<p><code>setAttributeNodeNS</code> adds a new namespaced attribute node to an element.</p>
<h3 id="Syntax" name="Syntax">Syntax</h3>
<pre class="eval"><em>replacedAttr</em> = element.setAttributeNodeNS(<em>attributeNode</em>) 
</pre>
<ul> <li><code>replacedAttr</code> is the replaced attribute node, if any, returned by this function.</li> <li><code>attributeNode</code> is an <code>Attr</code> node.</li>
</ul>
<h3 id="Example" name="Example">Example</h3>
<pre>// &lt;div id="one" xmlns:myNS="http://www.mozilla.org/ns/specialspace" 
            myNS:special-align="utterleft"&gt;one&lt;/div&gt; 
// &lt;div id="two"&gt;two&lt;/div&gt; 


var myns = "http://www.mozilla.org/ns/specialspace"; 
var d1 = document.getElementById("one"); 
var d2 = document.getElementById("two"); 
var a = d1.getAttributeNodeNS(myns, "special-align"); 
d2.setAttributeNodeNS(a.cloneNode(true));
alert(d2.attributes[1].value) // returns: `utterleft'
</pre>
<h3 id="Notes" name="Notes">Notes</h3>
<p>If the specified attribute already exists on the element, then that attribute is replaced with the new one and the replaced one is returned.</p>
<p>Note that if you try to set without cloning the node, Mozilla gives an NS_ERROR_DOM_INUSE_ATTRIBUTE_ERR "Attribute already in use" error, as the DOM requires cloning for Attr to be reused (unlike other Nodes which can be moved).</p>
<p>{{ DOMAttributeMethods() }}</p>
<h3 id="Specification" name="Specification">Specification</h3>
<p><a class="external" href="http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-ElSetAtNodeNS">DOM Level 2 Core: setAttributeNodeNS</a></p>
<p> </p>
<p>{{ languages( { "fr": "fr/DOM/element.setAttributeNodeNS", "ja": "ja/DOM/element.setAttributeNodeNS", "pl": "pl/DOM/element.setAttributeNodeNS" } ) }}</p>
Revert to this revision