Web/API/Element/getAttributeNames

From Get docs


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

Update compatibility data on GitHub

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
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