Get all Attributes of a DOM Element using JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 4, 2024
3 min

banner

# Get all Attributes of a DOM Element in JavaScript

To get all of the attributes of a DOM element:

  1. Use the getAttributeNames() method to get an array of the element's attribute names.
  2. Use the reduce() method to iterate over the array.
  3. On each iteration, add a new key-value pair containing the name and value of the attribute.

Here is the DOM element we will get the attributes of.

index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>bobbyhadz.com</title> </head> <body> <!-- ๐Ÿ‘‡๏ธ --> <div id="blue" data-id="example" class="box">Box 1</div> <script src="index.js"></script> </body> </html>
The code for this article is available on GitHub

And here is the related JavaScript code.

index.js
const element = document.getElementById('blue'); // โœ… Get object of all {name: value} const attrs = element.getAttributeNames().reduce((acc, name) => { return {...acc, [name]: element.getAttribute(name)}; }, {}); // ๐Ÿ‘‡๏ธ {id: 'blue', 'data-id': 'example', class: 'box'} console.log(attrs); console.log(attrs['id']); // ๐Ÿ‘‰๏ธ blue console.log(attrs['data-id']); // ๐Ÿ‘‰๏ธ example

get all attributes of dom element

The code for this article is available on GitHub

We used the Element.getAttributeNames() method to get an array of the element's attribute names.

index.js
const element = document.getElementById('blue'); // ๐Ÿ‘‡๏ธ ['id', 'data-id', 'class'] console.log(element.getAttributeNames());

The next step is to use the Array.reduce method to iterate over the array of attribute names.

We passed an empty object as the initial value for the accumulator variable.

The value we return from the callback function gets passed as the accumulator on the next iteration.

We assign the attribute name as a key on the object and use the name to get the corresponding attribute value via the Element.getAttribute() method.

index.js
const element = document.getElementById('blue'); // โœ… Get object of all {name: value} const attrs = element.getAttributeNames().reduce((acc, name) => { return {...acc, [name]: element.getAttribute(name)}; }, {}); // ๐Ÿ‘‡๏ธ {id: 'blue', 'data-id': 'example', class: 'box'} console.log(attrs);
After the last iteration, the object will contain a mapping of all the element's attribute names to the element's attribute values.

# Get all Attributes of a DOM Element as an Array

If you need to get an array of a DOM element's attribute values, you have to:

  1. Use the getAttributeNames() method to get an array of the element's attribute names.
  2. Use the map() method to iterate over the array.
  3. Get each attribute value using the getAttribute method.
index.js
const element = document.getElementById('blue'); // โœ… Get array of all attribute values const attrValues = element .getAttributeNames() .map(name => element.getAttribute(name)); console.log(attrValues); // ๐Ÿ‘‰๏ธ ['blue', 'example', 'box']

get all attributes of dom element as array

The code for this article is available on GitHub

It would have been more intuitive and direct if a getAttributeValues method existed, but this approach gets the job done.

The function we passed to the Array.map() method gets called for each element in the array of attribute names.

On each iteration, we use the getAttribute method to get the value of the attribute and return the result.

If you need to get an array containing the names of the element's attributes, use the getAttributeNames() method.

index.js
const element = document.getElementById('blue'); const attrNames = element.getAttributeNames(); console.log(attrNames); // ๐Ÿ‘‰๏ธ ['id', 'data-id', 'class']

I've also written an article on how to get elements by data attribute.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev