Languages

JavaScript - get HTML elements by part of classname or id?

0 points
Asked by:
ArcadeParade
666

How can I get HTML elements by part of classname or id using JavaScript?

I have a code similar to this:

Now what I want is to get the handle to all elements containing "my-element" inside id. and save them in an array.

I need something similar to:

but for all elements from 1-3.

1 answer
0 points
Answered by:
Admin Dirask Community
4380

You can use querySelectorAll() method.

Practical example:

 

References

  1. Document.querySelectorAll() - Web APIs | MDN
  2. ID selectors - CSS: Cascading Style Sheets | MDN
0 commentsAdd comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join