Skip to content
Advertisement

HTML Local Storage Dark Mode Using JavaScript

I tried to make a complete darkmode to my website. It’s almost done, but I have an issue. When I reload the page the dark mode goes back to the default light mode. How can I sovle this problem? I tried some code but it didn’t work. I want to use Local Storage, but I don’t know how to add it to my code. Can someone help me with this? Here is my sample code:

JavaScript
JavaScript

Advertisement

Answer

For your sample code, it seems like the best approach would be to add something to the darkmode function:

JavaScript
JavaScript

MDN localStorage reference: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement