Last updated: Mar 4, 2024
Reading timeยท3 min
To get all of the attributes of a DOM element:
getAttributeNames()
method to get an array of the element's
attribute names.reduce()
method to iterate over the array.Here is the DOM element we will get the attributes of.
<!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>
And here is the related JavaScript code.
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
We used the Element.getAttributeNames() method to get an array of the element's attribute names.
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.
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);
If you need to get an array of a DOM element's attribute values, you have to:
getAttributeNames()
method to get an array of the element's
attribute names.map()
method to iterate over the array.getAttribute
method.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']
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.
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.
You can learn more about the related topics by checking out the following tutorials: