Element.attributes[] (Collection)

A reference to a collection of attribute objects for the HTML tag that the Element object represents.

Availability:

DOM level - 1
JavaScript - 1.5
JScript - 5.0
Internet Explorer - 5.0
Netscape - 6.0
Property/method value type:Attributes object
JavaScript syntax:-myElement.attributes

This property yields an attributes object which is a collection of Attribute objects belonging to the object instantiated by an HTML tag.

The attributes object contains properties that JavaScript can inspect to obtain the value of HTML tag attributes for the tag that instantiated the Element object.

Structurally, the attributes object is an array whose elements correspond to properties of the Element object, each containing a name, a value and a flag. The Flag value is set true only when the property has a meaningful value. That value is reflected in the value property of the attribute object.

So, to find out all about an Element object property, you can get the value of the property by enquiring for its contents directly. However, if you use the property name to index the attributes array for that Element object, you can find out other information about that property by inspecting the attribute properties.

Access a property of an object like this:

myObject["propertyName"]

or like this:

myObject.propertyName

To access its corresponding attributes use this:

myObject.attributes["propertyName"].name

myObject.attributes["propertyName"].specified

myObject.attributes["propertyName"].value

However you should test for the existence of the myObject.attributes[myProp] object first because an attribute object is not created for every property of an Element object but only those that correspond to valid HTML tag attributes for the parent tag.

Any Element objects that represent HTML tags will have a nodeType of 1. Other node types supported by their attributes property will contain the null value.

The example code shows how this attribute access might be provided with a function.

Example code:

   // An attribute accessor function
   function getPropertyAttribute(anObject, aProp, anAttrib)
   {
      if(anObject.attributes[aProp])
      {
         return anObject.attributes[aProp][anAttrib];
      }
      else
      {
         return "";
      }
   }

See also:Attributes object, Element object, Element.mergeAttributes(), Node.attributes[]

Property attributes:

ReadOnly.