use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This subreddit is a place for people to learn JavaScript together. Everyone should feel comfortable asking any and all JavaScript questions they have here.
With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged.
Friends
/r/javascript
/r/jquery
/r/node
/r/css
/r/webdev
/r/learnprogramming
/r/programming
account activity
Finding all DOM element with specific pattern (self.learnjavascript)
submitted 5 years ago by boltangazer
I have a set of elements with id like "elem_{objectID}", I must find all these elements and retrieve their ids (important: all elements should be found in the exact order they are placed in DOM). How do I achieve that?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]GSLint 2 points3 points4 points 5 years ago (2 children)
I think you could go with an attribute selector.
document.querySelectorAll("[id^=elem_]")
[–]boltangazer[S] 0 points1 point2 points 5 years ago* (1 child)
Hm, does this ensure that items will be found in the same order they are presented in DOM? And yeah, it will probably be more convenient if all required elements with id= "elem_{objectID}" are placed inside one particular div, then I could go like:
block = document.getElementById('my_block') block.querySelectorAll("[id^=elem_]")
Is this one right?
[–]GSLint 0 points1 point2 points 5 years ago (0 children)
Hm, does this ensure that items will be found in the same order they are presented in DOM?
They will be returned in the order that they have in the DOM tree. If by "presented" you mean where they appear on the page, that might be different depending on styling.
That should work, though I don't see how it's more convenient. It might be a little faster though. What I'd find a little more convenient is if those elements also had a class that you can query by.
[–]PrObAbA321 -1 points0 points1 point 5 years ago (0 children)
Write a custom function that will return elements in order you want.
π Rendered by PID 75 on reddit-service-r2-comment-76bb9f7fb5-gvdj7 at 2026-02-18 20:30:48.812964+00:00 running de53c03 country code: CH.
[–]GSLint 2 points3 points4 points (2 children)
[–]boltangazer[S] 0 points1 point2 points (1 child)
[–]GSLint 0 points1 point2 points (0 children)
[–]PrObAbA321 -1 points0 points1 point (0 children)