Categories
JavaScript Answers

How to get all elements which name starts with some string with JavaScript?

Spread the love

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.

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 *