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 trial

JavaScript One Solution

Best practice question

On question 5 the treehouse solution uses .getElementsByClassName('container')[1] but functionally .querySelectorAll('.container')[1] is the same other than practicing using different ways of selection is it "better" to use one of the other? Or just stick to one form of selection?

3 Answers

Hi Thomas,

I personally use querySelectorAll more often because it’s more versatile. I can use more powerful selectors (ie. attribute selectors, combinators, etc).

getElementsByClassName will only return classes.

The other difference is that querySelectorAll returns a static list versus live list.

Static means that if you store the value of querySelectorAll to a variable, the variable will hold whatever the value of querySelectorAll was when the variable was declared. But if you store getElementsByClassName to a variable, the variable holds the value of whatever getElementsByClassName returns at the time the variable is called (so it would account for changes in the DOM).

Thanks that clears up a lot for me!

Thanks you guys! Helped me a bunch!