I have, the div
\'s where id looks like this_div_id_NUMBER
, all div
\'s has the different NUMBER
part. How I find all
querySelectorAll takes CSS selectors and returns a HTMLNodeList (a kind of array) of all the elements matching that css selector.
The css selector ^
(starts with) can be used for your purpose. Learn more about it in this article.
For your case, it would be document.querySelectorAll("[id^='this_div_id']");
Note that querySelectorAll
doesn't return a live node list. That means, any changes to the dom would not be updated live in the return value of the function.
Another method would to assign all your elements a specific class and then you can use getElementsByClassName
(which is much faster than querySelector).
var divs = document.getElementsByClassName("divClass");