Categories
JavaScript Answers

How to check if data attribute exist with plain JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *