W3cubDocs

/DOM

element.getAttributeNames

Element.getAttributeNames() 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 compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
Basic support 61 18 45 No 48 9
Mobile
Android webview Chrome for Android Edge Mobile Firefox for Android Opera for Android iOS Safari Samsung Internet
Basic support 61 61 ? 45 48 ? ?

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/element/getAttributeNames