Attributes object

Attributes object

Represents attributes of DOM element.

To access attributes collection of the element use its attributes property: element.attributes

Properties

length
integer, number of attributes of the element. Read-only property.
[index]
string, value of attribute at the index position.  Index here is either string (name of attribute) or integer (zero-based index of the attribute). Read-write index accessor.

Enumeration

for … in
for(var name in element.attributes) { /* loop body */ }

Executes body of the loop for attributes of the element. Value of name variable is text – name of the attribute.

Methods

name
(index) returns: string

Returns string – name of the attribute at index position. Index is zero-based integer value here.

clear
() returns: N/A

Clears collection of attributes of the element.

remove
(attr) returns: N/A

Removes attribute from the collection of attributes. Attr here is either string (name of attribute) or integer (zero-based index of the attribute).

exists
(attr) returns: true|false

Returns true if attr exists in the collection of attributes. Attr here is either string (name of attribute) or integer (zero-based index of the attribute).

addClass
(className1: string [, … classNameN: string ] )

Adds class name[s] to the class attribute of the element.

removeClass
(className1: string [, … classNameN: string ] )

Removes class name[s] from the class attribute of the element.

toggleClass
(className: string [, on: boolean ] )

Adds or removes class. If on is provided then adds the class if it is true and removes it if on is false. If no on provided removes class if it is not there, or adds it otherwise.

hasClass
(className: string ) : boolean

Returns true if the element has className defined.