Last updated: Mar 5, 2024
Reading timeยท3 min

Use the hasAttribute() method to check if an attribute exists.
The hasAttribute method returns true if the provided attribute exists,
otherwise, false is returned.
Here is the HTML for the examples.
<!DOCTYPE html> <html lang="en"> <head> <title>bobbyhadz.com</title> <meta charset="UTF-8" /> </head> <body> <div data-example="box" id="box" title="my box"> Box content </div> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
// โ checking if an `id` attribute exists on an element const box = document.getElementById('box'); if (box.hasAttribute('id')) { console.log('โ the id attribute exists'); console.log(box.getAttribute('id')); // ๐๏ธ "box" } else { console.log('โ๏ธ the id attribute does NOT exist'); } // ------------------------------------ // โ checking if a data-* attribute exists on an element console.log(box.hasAttribute('data-example'));

We used the hasAttribute() method to check if an attribute exists on an element.
const box = document.getElementById('box'); // ๐๏ธ true console.log(box.hasAttribute('data-example')); // ๐๏ธ true console.log(box.hasAttribute('id')); // ๐๏ธ true console.log(box.hasAttribute('title')); // ๐๏ธ false console.log(box.hasAttribute('ANOTHER'));
The method takes the name of the attribute as a parameter and returns a boolean result:
true if the attribute exists on the element.false if it doesn't exist.As shown in the code sample, the hasAttribute() method can also be used to
check if a data attribute exists on an element.
You can use the getAttribute() method to get the attribute if it exists.
const box = document.getElementById('box'); if (box.hasAttribute('id')) { console.log('โ the id attribute exists'); console.log(box.getAttribute('id')); // ๐๏ธ "box" } else { console.log('โ๏ธ the id attribute does NOT exist'); }
The getAttribute method returns the value of the provided attribute on the
element.
The same approach can be used with a data-* attribute.
const box = document.getElementById('box'); if (box.hasAttribute('data-example')) { console.log('โ the data-example attribute exists'); console.log(box.getAttribute('data-example')); // ๐๏ธ "box" } else { console.log('โ๏ธ the data-example attribute does NOT exist'); }

If the element doesn't have the attribute set, the method returns null or an
empty string, depending on the browser's implementation.
When using the getAttribute() method to get the value of a data attribute, the
whole name of the attribute has to be passed to the method.
You can also use the getAttribute() method to check if an attribute exists.
Here is the HTML for the example.
<!DOCTYPE html> <html lang="en"> <head> <title>bobbyhadz.com</title> <meta charset="UTF-8" /> </head> <body> <div data-example="box" id="box" title="my box"> Box content </div> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const box = document.getElementById('box'); const attributeValue = box.getAttribute('data-example'); console.log(attributeValue); // ๐๏ธ box if (attributeValue) { console.log('The attribute exists', attributeValue); } else { console.log('The attribute does NOT exist'); }

The getAttribute() method returns the value of the specified attribute if it
exists.
If the attribute doesn't exist, the method returns null or an empty string
depending on the browser's implementation.
We used an if statement to check if the value stored in the variable is
truthy.
Once we enter the if block, we can be sure that the attribute exists.
You can also directly access the value of the attribute through the variable.
If the attribute doesn't exist, the variable will store a null or an empty
string value, in which case the else block is run.
This approach can also be used with data-* or regular attributes.
You can learn more about the related topics by checking out the following tutorials: