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
attribute to set.
value to assign to the attribute.