attributes, NamedNodeMapNN 6 IE 5 DOM 1  

  

The object returned by the attributes property of every W3C DOM element object is a collection (array) of references to Attr (a.k.a. attribute) objects. An attribute type of node always has a name associated with it, which opens the way for methods of the collection of such nodes to access them directly by name, rather than iterating through the array in search of a matching node name. In the W3C DOM structure, the abstract representation of this array of named nodes is called the NamedNodeMap object, which shares some properties and methods of the IE attributes object. Since both objects refer to the same parts of a document tree, they are treated here together. A couple of other W3C DOM collections are also of the NamedNodeMap variety, but your primary contact with the NamedNodeMap in HTML documents is as a collection of Attr objects. Collection members are sorted in source code order.

 

There are more direct ways to access an attribute of an element (such as the getAttribute( ) or getAttributeNode( ) methods of all elements). The property and methods shown here, however, assume that your script has been handed a collection of attributes independent of their host element, and your processing starts from that point.

 
Object Model Reference
 
elementReference.attributes
 
Object-Specific Properties
 
length
 
Object-Specific Methods
 
getNamedItem( )getNamedItemNS( )item( )removeNamedItem( )
removeNamedItemNS( )setNamedItem( )setNamedItemNS( )
 
Object-Specific Event Handler Properties

None.

lengthNN 6 IE 5 DOM 1  

Read-only  

Returns the number of elements in the collection.

 
Example
 
var howMany = document.getElementById("myTable").attributes.length;
 
Value

Integer.

getNamedItem( )NN 6 IE 6 DOM 1  

getNamedItem("attributeName")

  

Returns a single Attr object corresponding to the attribute whose node name matches the parameter value.

 
Parameters
 
attributeName

String corresponding to the name portion of an attribute's name/value pair.

 
Returned Value

Reference to one Attr object. If there is no match to the parameter value, the returned value is null.

getNamedItemNS( )NN 6 IE n/a DOM 2  

getNamedItemNS("namespaceURI", "localName")

  

Returns a single Attr object with a local name and namespace URI that match the parameter values.

 
Parameters
 
namespaceURI

URI string matching a URI assigned to a label earlier in the document.

localName

The local name portion of the attribute.

 
Returned Value

Reference to one Attr object. If there is no match to the parameter values, the returned value is null.

item( )NN 6 IE 5 DOM 1  

item(index)

  

Returns a single Attr object corresponding to the element matching the index value.

 
Parameters
 
index

A zero-based integer corresponding to the specified item in source code order.

 
Returned Value

Reference to one Attr object. If there is no match to the index value, the returned value is null. Unlike some other collections in IE, a string index value is not allowed for the attributes object.

removeNamedItem( )NN 6 IE 6 DOM 1  

removeNamedItem("attributeName")

  

Removes from the collection a single Attr object corresponding to the attribute whose node name matches the parameter value.

 
Parameters
 
attributeName

String corresponding to the name portion of an attribute's name/value pair.

 
Returned Value

Reference to the removed Attr object. If there is no match to the parameter value, the returned value is null.

removeNamedItemNS( )NN 6 IE n/a DOM 2  

removeNamedItemNS("namespaceURI", "localName")

  

Removes from the collection a single Attr object whose local name and namespace URI match the parameter values.

 
Parameters
 
namespaceURI

URI string matching a URI assigned to a label earlier in the document.

localName

The local name portion of the attribute.

 
Returned Value

Reference to the removed Attr object. If there is no match to the parameter values, the method generates an error.

setNamedItem( )NN 6 IE 6 DOM 1  

setNamedItem(attrObjectReference)

  

Inserts a single Attr object into the current collection of attributes. If the destination of the attribute is an existing element, you may also use the setAttributeNode( ) method on the element to insert the Attr object. When the setNamedItem( ) method is invoked, the browser first looks for a match between the new attribute's name and existing attribute names within the collection. If there is a match, the new attribute replaces the original one; otherwise, the new attribute is added to the collection.

 
Parameters
 
attrObjectReference

A reference to an Attr node object created through document.createAttribute( ) or an Attr node from another element in the document tree.

 
Returned Value

Reference to an Attr object either created anew or referenced from elsewhere in the document tree.

setNamedItemNS( )NN 6 IE n/a DOM 2  

setNamedItemNS(attrObjectReference)

  

Inserts a single Attr object into the current collection of attributes. If the destination of the attribute is an existing element, you may also use the setAttributeNodeNS( ) method on the element to insert the Attr object. When the setNamedItemNS( ) method is invoked, the browser first looks for a match between the new attribute's pairing of local name and namespace URI and existing attribute local names and namespace URIs within the collection. If there is a match, the new attribute replaces the original one; otherwise, the new attribute is added to the collection.

 
Parameters
 
attrObjectReference

A reference to an Attr node object created through document.createAttribute( ) or an Attr node from another element in the document tree.

 
Returned Value

Reference to an Attr object either created anew or referenced from elsewhere in the document tree.