I want to make a button, who clears the localstorage for a user. using localStorage.clear();
but it don't work.
If you look the demo, it don't work.
JS code:
var money = 0;
//localStorage function
if(localStorage.money) money = localStorage.getItem('money');
document.getElementById("money").innerHTML = money;
//Clicking function
function moneyClick(number){
money = parseInt(money) + 10;
document.getElementById ("money").innerHTML = money;
localStorage.setItem('money', money);
}
//clear data function
function clearClick(number){
localStorage.clear();
}
Your code does clear
localStorage
, you need to refresh the page to see that though. In your code you can make it more clear by settingmoney
to0
and updating the DOM when you clearlocalStorage
: