ZVON > References > DOM2 Reference

setAttributeNode (method )

Owning interface and usage:  
Element.setAttributeNode(newAttr)

Member of these other interfaces:  
none

Description:  
Adds a new attribute.

Parameters:  
Attr newAttr  -  The Attr node to add to the attribute list.

Returns:  
Attr -  If the newAttr attribute replaces an existing attribute, the replaced Attr node is returned, otherwise null is returned.

Exceptions:  
DOMException WRONG_DOCUMENT_ERR
Raised if newAttr was created from a different document than the one that created the element.
DOMException NO_MODIFICATION_ALLOWED_ERR
Raised if this node is readonly.
DOMException INUSE_ATTRIBUTE_ERR
Raised if newAttr is already an attribute of another Element object. The DOM user must explicitly clone Attr nodes to re-use them in other elements.

Note:  
If an attribute with that name (nodeName) is already present in the element, it is replaced by the new one.


Example:
Text in the first DIV.
Some text in the second DIV.
Some text and element in the third DIV.
We can try another elements. It will be much more interesting.
Text in the last DIV.
Source:
   <div id="doc">
     <div>
       Text in the first DIV.
     </div>
     <div id="DDD" class="secondClass">
       Some text in the second DIV.
     </div>
     <div class="thirdClass">
       Some text and <span id="SSS">element</span> in the third DIV.
     </div>
     <div class="fourthClass">
       We can try <i>another elements</i>.
       It will be much more <b>interesting</b>.
     </div>
     <div>
       Text in the last DIV.
     </div>
   </div>
     
JavaScript:
  var main = document.getElementById('doc');
  var collection = main.getElementsByTagName('div');
  var elem = collection[0];
  var attr = collection[2].getAttributeNode('class');
  elem.setAttributeNode(attr);
  var output = elem.getAttributeNode('class').value;
Output:
desired your browser
thirdClass