Categories
JavaScript Answers

How to get element by a custom attribute using JavaScript?

Spread the love

Sometimes, we want to get element by a custom attribute using JavaScript.

In this article, we’ll look at how to get element by a custom attribute using JavaScript.

How to get element by a custom attribute using JavaScript?

To get element by a custom attribute using JavaScript, we use querySelector.

For instance, we write

const element = document.querySelector('[tokenid="14"]');

to get the element with tokenid attribute set to 14 with querySelector.

Conclusion

To get element by a custom attribute using JavaScript, we use querySelector.

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 *