Sometimes, we want to check if data attribute exist with plain JavaScript.
In this article, we’ll look at how to check if data attribute exist with plain JavaScript.
How to check if data attribute exist with plain JavaScript?
To check if data attribute exist with plain JavaScript, we use the hasAttribute
method.
For instance, we write
if (!object.hasAttribute("data-example-param")) {
// ...
}
to check if object
doesn’t have the data-example-param
property.
hasAttribute
returns true
if the property with the given name exists and false
otherwise.
Conclusion
To check if data attribute exist with plain JavaScript, we use the hasAttribute
method.