ZVON > References > DOM2 Reference

setNamedItem (method )

Owning interface and usage:  
NamedNodeMap.setNamedItem(arg)

Member of these other interfaces:  
none

Description:  
Adds a node using its nodeName attribute.

Parameters:  
Node arg  -  A node to store in this map.

Returns:  
Node -  If the new Node replaces an existing node the replaced Node is returned, otherwise null is returned.

Exceptions:  
DOMException WRONG_DOCUMENT_ERR
Raised if arg was created from a different document than the one that created this map.
DOMException NO_MODIFICATION_ALLOWED_ERR
Raised if this map is readonly.
DOMException INUSE_ATTRIBUTE_ERR
Raised if arg is an Attr that is already an attribute of another Element object. The DOM user must explicitly clone Attr nodes to re-use them in other elements.
DOMException HIERARCHY_REQUEST_ERR
Raised if an attempt is made to add a node doesn't belong in this NamedNodeMap.

Note:  
If a node with that name is already present in this map, 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 attrNode = main.childNodes[3].attributes;
  var attr = document.createAttribute('temp');
  attr.value = 'temporary';
  attrNode.setNamedItem(attr);
  var output = attrNode.getNamedItem('temp').nodeValue;
Output:
desired your browser
temporary