NamedNodeMap (W3C DOM Core object)

Spec
Version Depr.
DOM1 No
Browser support (more…)
IE5.5+ FF1.5+ SA1.3+ OP9+
Buggy Buggy Buggy Buggy

Example

var collection = node.attributes;

The example above creates a reference to the attributes collection of a Node. If there were an attribute in that collection called darth, it could be accessed as collection['darth'] or collection.getNamedItem('darth').

If we knew it was, say, the first item in the collection, it could also be accessed as collection[0]; but since a NamedNodeMap is not in any particular order, we generally won't know our item's index. The ability to access the members by index is designed to make it easy to iterate through a collection:

for(var i=0; i<collection.length; i++)
{
  doSomethingWith(collection[i]);
}

Description

The NamedNodeMap interface represents an unordered collection of items (such as nodes, or string values), indexed by name.

It's also possible to access the items in a NamedNodeMap by number (starting from zero), however the DOM does not specify their order.

A named node map is a live collection, which means that changes to the collection it represents are immediately reflected in the node map (as opposed to it being a static snapshot).

The nodeName is used as the name under which a node is stored in a node map, which implies that for certain kinds of node — where all instances have the same name (such as #text or #comment) — it's not possible to store more than one instance of such a node within a single node map, because the names would clash. The DOM specification considers this preferable to implementing aliases.

CAUTION: A node map is not an array

Even though a node map looks like an array, it isn't an array — although you can iterate through it and refer to its members like an array, you can't use Array methods like push or pop on it.

Compatibility

Internet Explorer Firefox Safari Opera
5.5 6.0 7.0 1.5 2.0 3.0 1.3 2.0 3.0 9.0 9.5
Buggy Buggy Buggy Buggy Buggy Buggy Buggy Buggy Buggy Buggy Buggy

Internet Explorer doesn't implement the following:

Additionally, Internet Explorer 5.5 in HTML doesn't implement the following (but does in XML):

Internet Explorer has a buggy implementation of setNamedItem.

In all versions of Internet Explorer in HTML, the item method identifies as being of type string, rather than function; however it still works correctly.

Firefox, Safari 1.3 and 2 and Opera have buggy implementations of getNamedItemNS and removeNamedItemNS.

All browsers have buggy implementations of removeNamedItem and setNamedItemNS (where they’re supported at all).

In this Section

User-contributed notes

There are no comments yet.

Related Products