Categories
JavaScript Answers

How to get HTML elements by their attribute names with JavaScript?

Spread the love

Sometimes, we want to get HTML elements by their attribute names with JavaScript.

In this article, we’ll look at how to get HTML elements by their attribute names with JavaScript.

How to get HTML elements by their attribute names with JavaScript?

To get HTML elements by their attribute names with JavaScript, we can use the querySelectorAll method.

For instance, we write

const els = document.querySelectorAll("[property]");
const els2 = document.querySelectorAll('[property="value"]');

to call querySelectorAll with "[property]" to select all the elements with the property attribute and return a node list with the elements.

And we call querySelectorAll with "[property="value"]" to select all the elements with the property attribute set to value and return a node list with the elements.

Conclusion

To get HTML elements by their attribute names with JavaScript, we can use the querySelectorAll 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 *