Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialfairest
Full Stack JavaScript Techdegree Graduate 19,303 PointsWhy not always use querySelector or querySelectorAll?
Why not always use querySelector or querySelectorAll?
3 Answers
Dmitry Polyakov
4,989 PointsIt's just a matter of preference. As for me for instance I like using getElementById as much as possible.
Jamie Reardon
Treehouse Project ReviewerDepends on your "needs", querySelectorAll gets all elements associated with the value you enter and groups them into an array like element, therefore you could for example, loop through them.
querySelector on the other hand, only returns the first element from the top of the DOM tree.
Tall Toad
2,746 PointsAbove comments makes sense to me now. I was getting the same results using querySelector/All, but Gill was using getElementsBy etc. I was confused why my code still worked. I used "for-in" loops instead of the "for" loop.
mafk Goose
170 Pointsmafk Goose
170 PointsI'm sure you figured this out by now but querySelector(All) are slower when dealing with a ginormous amounts of information.
It is faster to compute when dealing with specific selectors. Typically it doesn't make much of a difference.