Table of Contents

<HTMLElement>.getAttr()

Returns the attribute value of an Element. If the attribute does not exist, it will return an empty string.

Usage:
String = Element.getAttr(attrName)
Member of:
HTMLElement
Parameters:
attrName – the name of the attribute to return (required).
Returns:
The value of the attribute as String, or an empty string if the attribute either does not exist, or if it is an empty attribute (i.e. it has no value).
Notes:
  • To check if an empty attribute exists, use .hasAttr() instead.

Examples

Simple example:

const it = document.getElementById('item')
console.log(it.getAttr('foo'));

See also

More information