HTML DOM Attribute Object


In the HTML DOM (Document Object Model), all are nodes :

  • Documents are document nodes

  • All HTML elements are element nodes

  • All HTML attributes are attribute nodes

  • Text inserted into an HTML element is a text node

  • Comments are comment nodes

Attr object

In the HTML DOM, an Attr object represents an HTML attribute.

HTML attributes always belong to HTML elements.

NamedNodeMap object

In the HTML DOM, the NamedNodeMap object represents an unordered list of nodes.

We can access nodes in NamedNodeMap by node name.

Browser support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support Attr objects and NamedNodeMap objects.

Property / MethodDescription
attr.isIdThe isId property returns true if the property is of type ID, false otherwise.
attr.nameReturn property name
attr.valueSet or return property value
attr.specifiedReturn true if the property is specified, false otherwise
nodemap.getNamedItem()The specified attribute node returned from the node list.
nodemap.item()Return the node at the specified index number in the node list.
nodemap.lengthReturn the number of nodes in the node list.
nodemap.removeNamedItem()Delete the specified attribute node
nodemap.setNamedItem()Set the specified attribute node (by name)

DOM 4 Warning!!!

In the W3C DOM kernel, the Attr (attribute) object inherits all the attributes and methods of the node object.

In DOM 4, Attr (attribute) objects are no longer inherited from node objects.

For long-term code quality, you need to avoid using node object properties and methods in property objects:

Property / MethodReasons for avoid
attr.appendChild()Attribute has no child nodes
attr.attributesAttribute has no attribute
attr.baseURIUse document.baseURI instead
attr.childNodesAttribute has no child nodes
attr.cloneNode()Use attr.value instead
attr.firstChildAttribute has no child nodes
attr.hasAttributes()Attribute has no attribute
attr.hasChildNodesAttribute has no child nodes
attr.insertBefore()Attribute has no child nodes
attr.isSupported()Usually true
attr.lastChildAttribute has no child nodes
attr.nextSiblingAttribute has no siblings
attr.nodeNameUse attr .name instead
attr.nodeTypeUsually 2 (ATTRIBUTE-NODE)
attr.nodeValueUse attr .value instead
attr.normalize()Attribute has no specification
attr.ownerDocumentUsually your HTML document
attr.ownerElementThe HTML element you use to access the attribute
attr.parentNodeThe HTML element you use to access the attribute
attr.previousSiblingAttribute has no siblings
attr.removeChildAttribute has no child nodes
attr.replaceChildAttribute has no child nodes
attr.textContentUse attr .value instead