The NamedNodeMap Object


The NamedNodeMap object is another collection of nodes, the difference being that this collection is accessible by name.

Table 16.7 shows the NamedNodeMap object properties.

Table 16.7 NamedNodeMap Attributes/Properties

Name Value

length The number of nodes in the NamedNodeMap

Listing 16.8 shows a JavaScript example using this property.

Listing 16.8 Getting the Length of a NamedNodeMap


1:  //var node holding a Node Object
2:  if (node.attributes != null)
3:  //testing if the node has attributes
4:       return node.attributes.length;
5:  //returns the number of attributes

Table 16.8 shows the methods of the NamedNodeMap object.

Table 16.8 NamedNodeMap Methods

Name Returns

getNamedItem(name) A node with the specified name (string) or null
setNamedItem(arg) Nothing, but adds a node specified in arg to the NamedNodeMap
removeNamedItem(name) The removed node with the specified name (string)
item(n) The nth item of the collection

Listing 16.9 shows a JavaScript example using the getNamedItem() method.

Listing 16.9 Using the getNamedItem() Method of a NamedNodeMap Object/Interface


1:  //var node holds a Node Object
2:  return node.attributes.getNamedItem("unit");
3:  //returns the node of type attribute with name "unit"

A DocumentFragment is an object that can contain pieces of documents (document fragments) for copying and pasting, rearranging, and so on. It inherits from the Node object class.