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 trialPavel Rogulin
5,036 PointsCan't solve task 1(Practice Selecting Elements)
I need help. First I tried to use getElementsByTagName(‘nav’)
but it returned “Was expecting 3 links not 1”, then added class=’selected’
to all 3 <li>
elements and tried to use getElementsByTagName(‘selected’)
and again “Was expecting 3 links not 1”. Then I tried querySelectorAll(‘.selected’)
and… Yes, it was “Was expecting 3 links not 1”. What am I doing wrong?
1 Answer
Austin Whipple
29,725 PointsFor this challenge question, there should be no need to modify the HTML. You'll just need to adjust how you're looking for the anchor tags within the nav
element. document.getElementsByTagName('nav')
will only select nav
elements, no child or grandchildren of them. In this case you're looking for elements that are many branches away, so the links would never make their way into that array.
A more flexible method is document.querySelectorAll()
since it allows you to use normal CSS selector rules. So you could, for instance, look for nav a
. Be careful to avoid document.querySelector
, however, because that selects only the first matched element. And again, no need to modify the HTML, so look for other ways to target all those elements with modifying their existing classes.
Give this video and the teachers notes a quick review and try again!
Austin Whipple
29,725 PointsAustin Whipple
29,725 PointsOh! And check out this CSS Selectors Reference for all the ways you can use
document.querySelectorAll()