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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *