Reference attribute()

attribute()

Adds an attribute to the element.

This method is useful for advanced tasks. Most commonly-used attributes, such as id, can be set with their dedicated methods. For example, nextButton.id('next') sets an element's id attribute. Calling nextButton.attribute('id', 'next') has the same effect.

The first parameter, attr, is the attribute's name as a string. Calling myElement.attribute('align') returns the attribute's current value as a string or null if it hasn't been set.

The second parameter, value, is optional. It's a string used to set the attribute's value. For example, calling myElement.attribute('align', 'center') sets the element's horizontal alignment to center.

Examples

Syntax

attribute()
attribute(attr, value)

Parameters

attr
String:

attribute to set.

value
String:

value to assign to the attribute.

Returns

String: value of the attribute.
Notice any errors or typos? Please let us know. Please feel free to edit src/dom/dom.js and open a pull request!

Related References