HTML DOM Attribute Objects

# The HTML DOM Attribute Object

« Previous
Next Reference »

## HTML DOM Nodes

In the HTML DOM (Document Object Model), everything is a node:

- The document itself is a document node
- All HTML elements are element nodes
- All HTML attributes are attribute nodes
- Text inside HTML elements are text nodes
- Comments are comment nodes

## The Attr Object

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

An HTML attribute always belongs to an HTML element.

## The NamedNodeMap Object

In the HTML DOM, the NamedNodeMap object represents an unordered collection of an elements attribute nodes.

Nodes in a NamedNodeMap can be accessed by name or by index (number).

## Browser Support

Object |  |  |  |  |
--- | --- | --- | --- | --- | ---
Attr | Yes | Yes | Yes | Yes | Yes
NamedNodeMap | Yes | Yes | Yes | Yes | Yes

The Attr Object and the NamedNodeMap Object is supported in all major browsers.

## Properties and Methods

Property / Method | Description
--- | ---
attr.isId | Returns true if the attribute is of type Id, otherwise it returns false
attr.name | Returns the name of an attribute
attr.value | Sets or returns the value of the attribute
attr.specified | Returns true if the attribute has been specified, otherwise it returns false
nodemap.getNamedItem() | Returns a specified attribute node from a NamedNodeMap
nodemap.item() | Returns the attribute node at a specified index in a NamedNodeMap
nodemap.length | Returns the number of attribute nodes in a NamedNodeMap
nodemap.removeNamedItem() | Removes a specified attribute node
nodemap.setNamedItem() | Sets the specified attribute node (by name)

## DOM 4 Warning !!!

In the W3C DOM Core, the Attr (attribute) object inherits all properties and methods from the Node object.

In DOM 4, the Attr object no longer inherits from Node.

For future code quality, you should avoid using node object properties and methods on attribute objects:

Property / Method | Reason for avoiding
--- | ---
attr.appendChild() | Attributes don't have child nodes
attr.attributes | Attributes don't have attributes
attr.baseURI | use document.baseURI instead
attr.childNodes | Attributes don't have child nodes
attr.cloneNode() | Get or set the attr.value instead
attr.firstChild | Attributes don't have child nodes
attr.hasAttributes() | Attributes don't have attributes
attr.hasChildNodes | Attributes don't have child nodes
attr.insertBefore() | Attributes don't have child nodes
attr.isEqualNode() | Makes no sense
attr.isSameNode() | Makes no sense
attr.isSupported() | Is always true
attr.lastChild | Attributes don't have child nodes
attr.nextSibling | Attributes don't have siblings
attr.nodeName | Use attr.name instead
attr.nodeType | This is always 2 (ATTRIBUTE_NODE)
attr.nodeValue | Use attr.value instead
attr.normalize() | Attributes cannot be normalized
attr.ownerDocument | This is always your HTML document
attr.ownerElement | This is the HTML element you used to access the attribute
attr.parentNode | This is the HTML element you used to access the attribute
attr.previousSibling | Attributes don't have siblings
attr.removeChild | Attributes don't have child nodes
attr.replaceChild | Attributes don't have child nodes
attr.textContent | Use attr.value instead

« Previous
Next Reference »

### WEB HOSTING

UK Reseller Hosting

### WEB BUILDING

FREE Website BUILDER Free HTML5 Templates

### W3SCHOOLS EXAMS

HTML, CSS, JavaScript, PHP, jQuery, XML, and ASP Certifications

### SHARE THIS PAGE

### Color Picker

### HTML/CSS

HTML Tutorial HTML5 Tutorial CSS Tutorial CSS3 Tutorial Bootstrap Tutorial

### JavaScript

JavaScript Tutorial jQuery Tutorial jQuery Mobile Tutorial AngularJS Tutorial AJAX Tutorial JSON Tutorial

### Graphics

Canvas Tutorial SVG Tutorial Google Maps Tutorial

### Server Side

SQL Tutorial PHP Tutorial ASP Tutorial ASP.NET Tutorial VBScript Tutorial AppML Tutorial

### Web

Web Building Web Statistics Web Certification

### XML

XML Tutorial DTD Tutorial Schema Tutorial XML DOM Tutorial XPath Tutorial XSLT Tutorial XQuery Tutorial XSL-FO Tutorial RSS Tutorial WSDL Tutorial

### HTML/CSS

HTML Tag Reference HTML Event Reference HTML Color Reference CSS 1,2,3 Reference Bootstrap Reference

### JavaScript

JavaScript Reference HTML DOM Reference jQuery Reference jQuery Mobile Reference Google Maps Reference

### Server Side

PHP Reference SQL Reference ASP Reference VBScript Reference Razor Reference ASP.NET Reference

### XML

XML DOM Reference XSLT Reference XSL-FO Reference XPath Reference XQuery Reference Schema Reference RSS Reference SVG Reference

### HTML Charsets

HTML Character Sets HTML ASCII HTML ANSI HTML Windows-1252 HTML ISO-8859-1 HTML Symbols HTML UTF-8

### HTML/CSS

HTML Examples CSS Examples

### Browser Scripting

JavaScript Examples HTML DOM Examples jQuery Examples jQuery Mobile Examples AngularJS Examples AJAX Examples

### Server Scripting

PHP Examples ASP Examples VBScript Examples Razor Examples .NET Examples

### XML Languages

XML Examples DTD Examples XML DOM Examples XSL Examples XSLT Examples XPath Examples XQuery Examples Schema Examples RSS Examples SVG Examples