Accessing Attribute Nodes

Contact Us or call 1-877-932-8228
Accessing Attribute Nodes

Accessing Attribute Nodes

The element.getAttribute() Method

The getAttribute() method of an element node returns the attribute value as a string or null if the attribute doesn't exist. The syntax is shown below:

myNode.getAttribute("AttName");

Most HTML attributes are also directly available as properties of the element node; for example, for an img tag element, the width, height, and source are available as the width, height, and src properties.

The element.attributes[] Property

The attributes[] property references the collection of a node's attributes. On the face of it, such a collection could be very useful; however, older versions of Internet Explorer include all possible attributes of a node in its attributes collection rather than just those attributes currently used by the node. As such, in practice, it's better to access attributes with the getAttribute() method.

Next