Sometimes, we want to find all elements whose id begins with a common string with JavaScript.
In this article, we’ll look at how to find all elements whose id begins with a common string with JavaScript.
How to find all elements whose id begins with a common string with JavaScript?
To find all elements whose id begins with a common string with JavaScript, we call querySelectorAll
.
For instance, we write
const dates = document.querySelectorAll('[id^="createdOnId"]');
to call querySelectorAll
with '[id^="createdOnId"]'
to return a node list with all elements that has ID starting with createdOnId
.
Conclusion
To find all elements whose id begins with a common string with JavaScript, we call querySelectorAll
.