Open veraldik opened 5 years ago
Hi @veraldik, happy to help! π
First, let's move all of the JavaScript into your mode.js
file and use that file in your HTML by adding this line:
<script src="mode.js"></script>
This will give us better ways to troubleshoot if needed.
Another thing I noticed is that I see the following error in the Chrome Devtools console when I click the button on your page:
localStorage.remove
isn't a function. For this project, you also don't need to worry about clearing anything from localStorage
, so go ahead and remove both window.localStorage.remove('theme')
statements. That should resolve this error that stops your code from running.
The next thing I recommend is replacing your else if
with an else
. This way, if anything besides day-theme
ever finds its way into localStorage
, your code will still switch the theme correctly.
From what I can see, I think the button will work correctly after the above changes. The final part is to set the theme when the page loads by getting the saved theme from localStorage
.
To do that, write some code that does the following at the very end of your JavaScript file:
localStorage
and put it in a variablenull
, set the body
style to day-theme
null
, set the body
style to the value of the variableThis should complete the theme requirements. Give these recommendations a try and send some questions my way if you get stuck again. π
Ah, thank you. It finally works haha
You're welcome! Your code looks and works really well. Nice work incorporating those recommendations and pulling the rest together. I don't have any recommendations for improvement! π :shipit:
Build a website that allows the user to switch between day and night color themes and remembers their choice
@egillespie Can you take a look at this? It's hosted here and meets the following criteria:
body
element is always up-to-date with the current theme (eitherday-theme
ornight-theme
) -- you can usedocument.body.setAttribute('class', 'some-class-name')
to set the class on thebody
1
the first time)localStorage
night-theme
, the background of the website is a very dark color, with a lighter color for the textday-theme
, the background of the website is light-colored, with a darker color for the textI am so stuck! I know i'm missing parts of the project but I'm just trying to get the base firgured out right now. Everytime I refresh the page in firefox (using live-server) different things happen with the button. Please help ):