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 trialSadaf Partovi
Front End Web Development Techdegree Graduate 14,594 PointsHow can we decide which method to use when selecting an element.
Here it wants us to "Set the content of the '.desc' paragraph" in the video it uses "document.querySelector(".desc");". My question is why document.getElementsByClassName("desc"); won't work???
2 Answers
Christine Bogdan
11,349 PointsHi there,
querySelector(".desc") returns the FIRST element in the document, that matches the specified selector. In contrast to that, getElementsByClassName returns an array of ALL elements in the document, that match the specified selector. If you want to access a specific element in that array, you need to use its index.
Hope this helps! :)
JASON LEE
17,352 PointsI got mine to work using the getElementsByClassName("desc") with the only caveat to add a suffix to specify the array element or index, so instead of getElementsByClassName("desc"), I used getElementsByClassName("desc")[0]
Sadaf Partovi
Front End Web Development Techdegree Graduate 14,594 PointsSadaf Partovi
Front End Web Development Techdegree Graduate 14,594 PointsThanks for responding :)