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

Randall Brownlee
Randall Brownlee
5,518 Points

Code works in the console, but exact same code wont work when previewed through Workspace. Issue with browser?

const purpleList = document.getElementsByTagName('li'); for(let i=0; i<purpleList.length; i++) { purpleList[i].style.color = 'purple' };

Works when typed directly into console, but wont work through workspace. Does anyone know if this is an issue with the way Chrome is loading the javascript from workspaces?

Post a snapshot of your workspace. Don't know how? see this link:

https://teamtreehouse.com/community/how-to-post-a-snapshot-onto-a-question

6 Answers

I Dilate
I Dilate
3,983 Points

Hi Randall,

I just tried the code you posted in a Workspace, and it's working with no errors in the console.

I can't spot anything that's wrong with it syntactically, so I guess this could be a problem with your browser - maybe you should try closing the whole project down and start it up again in order to make sure you're previewing the right files, or clear your cache to make sure an older version isn't being loaded.

Alternatively there may be something else going wrong - could even be a simple problem with the HTML it's connected to. Is your HTML file definitely calling your script, for example?

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Generally speaking, when something you're positive should be working in the browser isn't, it's generally to do with the browser. In terms of the front-end courses, workspaces is just a handy text-editor. That code is being run locally in your browser.

Here's a few things to check:

  • Make sure you've saved your file (Silly, I know, but we all miss it sometimes)
  • Try clearing your browser cache and refreshing the page (it could be loading in an older version of your code)

If neither of these is the problem, we're going to need to take a look at your workspace. This is most easily accomplished by linking us to a snapshot.

Let me know how it goes! :sparkles:

I Dilate
I Dilate
3,983 Points

Hi, this is a message for @alee2,

I think because this question is classed as answered, it's closed for comments now. So although those of us that have answered are getting an email notification of your question, your comment isn't appearing here.

If you want to ask again - you should create a new question and paste the code specific to your use.

That said, I've taken a quick look at the snapshot of your Workspace, and you've commented out the button, and the <script> tags that load in app.js... you'll see tags that look like this <!-- --> around the important bits.

When you remove the comment tags, its loading app.js and all of the links turn yellow, because your loop is working correctly.

I suggest you take a step back and review the basics of HTML before proceeding with Javascript. You really must understand what basic elements like comments do before you move into the interaction layer provided by JavaScript.

When you take out the comments and check it's working, its probably a good idea to return to unused.js and work through the other parts of the lesson.

Good luck, you'll get there! Rich

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

To be clear, I'm not aware of any situation other than a deleted question where the result is that it will be closed for comments. Also, I have not received any notifications from a student named @alee2 on this thread.

Ya, I only received notifications for activity from I Dilate and Jennifer Nordell

Very much appreciate your reply.
The code snippet i pasted was from app.js, not unused.js unused.js was just that--unused--and not actually linked to index.html. neither was app.js which was the problem. facepalm.

I Dilate
I Dilate
3,983 Points

It's been resolved, the question was deleted. Sorry for causing the confusion guys.

Sorry for the confusion, All. I Deleted -- because I discovered my link to app.js in index.html didn't save. doh. eyeroll.

I Dilate
I Dilate
3,983 Points

LOL no worries, glad you sorted it out alee2!