Categories
JavaScript Answers

How to find all elements whose id begins with a common string with JavaScript?

Spread the love

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.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.