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 |
---|---|---|
DOMThe 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.
Update compatibility data on GitHub
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.1 |
WebView Android
Full support 61 |
Chrome Android
Full support 61 |
Firefox Android
Full support 45 |
Opera Android
Full support 45 |
Safari iOS
Full support 10.3 |
Samsung Internet Android
Full support 8.0 |
Legend
- Full support
- Full support
- No support
- No support
Element.getAttributeNames() by Mozilla Contributors is licensed under CC-BY-SA 2.5.