Sometimes, we want to get all elements which name starts with some string with JavaScript.
In this article, we’ll look at how to get all elements which name starts with some string with JavaScript.
How to get all elements which name starts with some string with JavaScript?
To get all elements which name starts with some string with JavaScript, we call querySelectorAll
.
For instance, we write
const els = document.querySelectorAll("[name^=q1_]");
to call querySelectorAll
to select all elements with the name attribute starting with q1_ in document
.
Conclusion
To get all elements which name starts with some string with JavaScript, we call querySelectorAll
.