HTML DOM Attribute Object
HTML DOM node
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
All major browsers support Attr objects and NamedNodeMap objects.
Property / Method | Description |
---|---|
attr.isId | The isId property returns true if the property is of type ID, false otherwise. |
attr.name | Return property name |
attr.value | Set or return property value |
attr.specified | Return 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.length | Return 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 / Method | Reasons for avoid |
---|---|
attr.appendChild() | Attribute has no child nodes |
attr.attributes | Attribute has no attribute |
attr.baseURI | Use document.baseURI instead |
attr.childNodes | Attribute has no child nodes |
attr.cloneNode() | Use attr.value instead |
attr.firstChild | Attribute has no child nodes |
attr.hasAttributes() | Attribute has no attribute |
attr.hasChildNodes | Attribute has no child nodes |
attr.insertBefore() | Attribute has no child nodes |
attr.isEqualNode() | Pointless |
attr.isSameNode() | Pointless |
attr.isSupported() | Usually true |
attr.lastChild | Attribute has no child nodes |
attr.nextSibling | Attribute has no siblings |
attr.nodeName | Use attr .name instead |
attr.nodeType | Usually 2 (ATTRIBUTE-NODE) |
attr.nodeValue | Use attr .value instead |
attr.normalize() | Attribute has no specification |
attr.ownerDocument | Usually your HTML document |
attr.ownerElement | The HTML element you use to access the attribute |
attr.parentNode | The HTML element you use to access the attribute |
attr.previousSibling | Attribute has no siblings |
attr.removeChild | Attribute has no child nodes |
attr.replaceChild | Attribute has no child nodes |
attr.textContent | Use attr .value instead |