HTML CSS Bootstrap Javascript Icons Python
JavaScript Reference
Overview
JS Reference
JS Array Object JS Boolean Object JS Date Object JS Style Declaration Object JS Error Object JS Global Object JS Math Object JS Number Object JS String Object JS RegExp Object JS Statements
DOM Reference
DOM Attribute DOM Console DOM Document DOM Element DOM Events DOM Event Objects DOM History DOM HTMLCollection DOM Location DOM Navigator DOM Screen DOM Style DOM Window WEB Storage

HTML DOM Attribute Object

The Attr Object

The Attr object represents an attribute in an Element object.

An HTML attribute always belongs to an HTML element.

In most DOM methods, you will probably directly retrieve the attribute as a string (e.g., Element.getAttribute(), but certain functions (e.g., Element.getAttributeNode()) or means of iterating give Attr types.


The NamedNodeMap Object

The NamedNodeMap object represents an unordered collection of Attr objects.

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


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)

Examples

This example displays all attribute names of the IMG element:

Run code

This example displays all attribute values of the IMG element:

Run code

This example change the value of the src attribute of the IMG element:

Run code