== .setAttr==

.setAttr()

Sets an attribute value of an Element. If the attribute does not exist, it will be created.
Usage:
[[mdn>Web/API/HTMLElement|HTMLElement]] = Element.''setAttr''(attrName, attrValue)
Member of:
[[mdn>Web/API/HTMLElement|HTMLElement]]
Parameters:
- attrName – the name of the attribute to set/change (required). - attrValue – the value to set (required).
Returns:
The [[mdn>Web/API/HTMLElement|HTMLElement]] it was called on, thus allowing for command chaining.
===== Examples ===== Simple example: const it = document.getElementById('item'); it.setAttr('foo', 'bar')); ===== See also ===== * [[toolbox:element:getattr:index|.getAttr()]] * [[toolbox:element:hasattr:index|.hasAttr()]] ===== More information ===== * [[mdn>Web/API/Element/setAttribute|Element: setAttribute() method]] on MDN.