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.