ZVON > References > DOM1 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 a named node map. If a node with that name is already present in the map, it is replaced by the new one.

Returns:  
Node -  If the new Node replaces an existing node with the same name the previously existing 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 the NamedNodeMap.
DOMException NO_MODIFICATION_ALLOWED_ERR
Raised if this NamedNodeMap 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.

Note:  
Remember that multiple nodes of certain types (those that have a "special" string value) are not allowed.


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