The getAttributeNames()
method of the Element
interface returns the attribute names of the element as an Array
of strings. If the element has no attributes it returns an empty array.
Using getAttributeNames()
along with getAttribute()
, is a memory-efficient and performant alternative to accessing Element.attributes
.
Syntax
let attributeNames = element.getAttributeNames();
Example
// Iterate over element's attributes for (let name of element.getAttributeNames()) { let value = element.getAttribute(name); console.log(name, value); }
Polyfill
if (Element.prototype.getAttributeNames == undefined) { Element.prototype.getAttributeNames = function () { var attributes = this.attributes; var length = attributes.length; var result = new Array(length); for (var i = 0; i < length; i++) { result[i] = attributes[i].name; } return result; }; }
Specifications
Specification | Status | Comment |
---|---|---|
DOM The definition of 'Element.getAttributeNames' in that specification. |
Living Standard | Initial definition |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
getAttributeNames | Chrome Full support 61 | Edge Full support 18 | Firefox Full support 45 | IE No support No | Opera Full support 48 | Safari Full support 10 | WebView Android Full support 61 | Chrome Android Full support 61 | Firefox Android Full support 45 | Opera Android Full support 45 | Safari iOS Full support Yes | Samsung Internet Android Full support 8.0 |
Legend
- Full support
- Full support
- No support
- No support