I want to have a button that toggles something in and out of localStorage (it's clicked once and it adds something to localStorage, it's clicked again and that something disappears from localStorage). I want to do something like the code below, but toggling "status" instead of setting it permanently.
Okay. I don't know I got you or not. Here is a solution..
First I'm taking a button :
<button>Toggle</button>
Then using some scripts to toggle (add, remove) the item inside of localsotrage.
<script>
const button = document.querySelector("button");
button.addEventListener("click", function(){
const item = localStorage.getItem('info');
if (!item){
localStorage.setItem('info', 'Hello World')
}else{
localStorage.removeItem('info')
}
})
</script>
inside of this script simple method has been implemented. At first I'm taking a value from localstorage. Inside of if/else condition when item value if falsy it will add item into localstorage. When it is truthy it will remove from localStorage.