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 trialMarc Sanchez
20,039 PointsWhy would I use JS to change text color instead of CSS? It's more code with JS and takes my space.
It takes more time and space to change text color using JS. We can get the same results using CSS. In what scenarios would it be better to do this in JS?
const myList = document.getElementsByTagName('li');
for ( let i = 0; i < myList.length; i++) { myList[i].style.color = 'purple'; }
vs
li { color: red; }
1 Answer
rydavim
18,814 PointsYou would use JavaScript when you want the CSS - in this case text color - to change as a reaction to something. JavaScript is meant to add interactivity and dynamic behavior, so as you get into it more the examples will be more relevant.
But yes, you would normally change the text color in this case using CSS.
Marc Sanchez
20,039 PointsMarc Sanchez
20,039 PointsThank you @rydavim.