![Check if an element has an attribute](https://javascriptsource.com/wp-content/uploads/2021/12/Check-if-an-element-has-an-attribute-702x526.jpg)
Check for the existence of an attribute (including data attributes) on an element. This method can also be used to manipulate other types of attributes—things like id
, tabindex
, name
, and so on.
var elem = document.querySelector('#lunch');
if (elem.hasAttribute('data-drink')) {
console.log('Add a drink!');
}
Source
https://vanillajstoolkit.com/reference/attributes/hasattribute/