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 JavaScript and the DOM (Retiring) Getting a Handle on the DOM Select All Elements of a Particular Type

It doesn't work in my app.js file but works in console. Why?

const myList = document.getElementsByTagName('li');

for ( let i = 0; i < myList.length; i += 1 ) { myList[i].style.color = 'red'; }

Can be a problem setting a:

<script src="app.js"> </script>

not in the last position in body?

Thank you

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,252 Points

It could be. The reason we write script tags as the last element(s) before the closing body tag is to ensurre that the browser has loaded all the DOM elements it needs before it looks for the JavaScript. That should rule out any exceptions for elements that don't yet exist on browser load.

1 Answer

Thank you