Skip to content
Advertisement

Save Toggle View State using Local Storage in Vanilla JavaScript

I am toggling between two views using LocalStorage. I can see the my set key and value in localstorage when I click the toggle button. But when I refresh, I lose the current view. What changes can I make to the code to save view in localStorage?

JavaScript

Advertisement

Answer

I have refactored your code so that you can achieve to render the current view:

script.js

JavaScript

styles.css

JavaScript

The script works with this HTML file:

index.html

JavaScript
Advertisement