Element

Element.prototype.getAttributeNames

Returns an array of attribute names of the element

Syntax

JavaScript
element.getAttributeNames()

Return Value

An Array of strings representing the attribute names

Examples

Basic Usage
const el = document.querySelector('input')!
const attrs = el.getAttributeNames()
console.log(attrs)
Practical Example
function copyAttributes(source: Element, target: Element) {
  source.getAttributeNames().forEach(name => {
    target.setAttribute(name, source.getAttribute(name)!)
  })
}
Advanced Usage
const el = document.querySelector('[data-config]')!
const dataAttrs = el.getAttributeNames().filter(n => n.startsWith('data-'))
console.log(dataAttrs)

Understanding Element.prototype.getAttributeNames

The Element.prototype.getAttributeNames method in JavaScript returns an array of attribute names of the element. It belongs to the Element object and is one of the most widely used methods for working with element values in modern JavaScript and TypeScript applications.

The method signature is element.getAttributeNames(). When called, it returns an array of strings representing the attribute names. Understanding when and how to use getAttributeNames() helps you write more expressive, readable code.

Common use cases for Element.prototype.getAttributeNames include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like dom-getattribute, dom-setattribute, dom-hasattribute, enabling you to chain operations together for complex data manipulation pipelines.

Browser support for Element.prototype.getAttributeNames is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.

Browser Compatibility

Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.

Related Methods

More Element Methods

Other methods in the Element object

Related Tools

More Element Methods

Explore JavaScript Methods

Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.