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 trialJonathan Fuentes
Full Stack JavaScript Techdegree Student 11,191 PointsNeed help understanding querySelectorAll
In this video, I'm having a hard time understanding why querySelectorAll doesn't work, like for example in number 2. Why do I need to loop through the TagName? Shouldn't querySelectorAll select them all or am I miss understanding its proper use?
2 Answers
Niclas Hilmersson
8,296 PointsI hope i understood your question correctly.
querySelectorAll can be used to target all of the tagnames as in the number two example. Why he isn't using it is for the practice itself. The task is mainly about using all variations of ways to target different elements. you could basically only use the queryselectors and it will work just fine. But to explain it further queryselectorall is very useful when you want to target several elements but be selective about which of the selected items you want. As an example you want to target all links with the classname of title. that will target only links that have that classname. if using the getElementsByClassName that will target ALL different elements with that classname not just the links.
seth aruby
10,111 PointsJonathan, technically he could have used querySelectorAll as well, and so could you! However, you will still need to iterate through the results of either b/c both of these queries will return a collection of results. If you were using a query that only returned one result (such as document.querySelector) then you wouldn't have had to iterate through multiple results. Does that make sense?