Sometimes, we want to get elements by attribute when querySelectorAll with JavaScript.
In this article, we’ll look at how to get elements by attribute when querySelectorAll with JavaScript.
How to get elements by attribute when querySelectorAll with JavaScript?
To get elements by attribute when querySelectorAll with JavaScript, we can use the attribute selector.
For instance, we write
const els = document.querySelector('[role="button"]')
to call querySelector
with '[role="button"]'
to select all the elements that has the role attribute set to button
.
Conclusion
To get elements by attribute when querySelectorAll with JavaScript, we can use the attribute selector.