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 trialwwyattw
10,389 PointsIn real practice, how often do you change the design/color via JS?
I understand the point of this video is to demonstrate ways you can use JS but in real practice often do you use JS to change the design? Would it be easier to change it on CSS file?
2 Answers
Steven Parker
231,269 PointsIf the design is static, you would indeed most likely implement it in CSS.
JavaScript is primarily used to make changes happen while the page is being viewed, perhaps in response to some action by the user.
I Dilate
3,983 PointsHi wwyattw,
Using JavaScript to change a colour of an element is probably not a great idea in most genuine use case scenarios, because it won't lead to developing an easily updatable codebase for your project.
It's more likely that you'll establish CSS rules for each state of an element - say, a "front" and a "reverse" state for a div you want to turn into a card that flips over when a user clicks it. You'll then just be using JS to switch between the two.
However, during the process of building a project, I often use JS's ability to make a colour/design change in order to take a step closer toward that final output. It helps me check something is working as I expect.
Furthermore, if you're coding something that's very simple - like a script to highlight any element that your mouse hovers over, then it may be easier just to define the colour changes right inside the JavaScript rather than worry about defining CSS styles for it.
I guess it depends on how you want to organise your code - the beauty of JavaScript is that it's very flexible and has control over all of the elements on your page that HTML and CSS do... it's up to you to decide how to use it.