Reputation: 449
I am trying to make a simple program with localStorage
. I created a button to reset all the data in the localStorage
. Unfortunately, it won't work I don't know why.
Here's my code:
<script>
var slot = localStorage.getItem("slot");
if (slot == null) {
slot = 10;
}
document.getElementById("slot").innerText = slot;
function reduceSlot() {
if (slot >= 1) {
slot--;
document.getElementById("slot").innerText = slot;
localStorage.setItem("slot", slot);
}
else {
document.getElementById('slot').innerText = "FULL";
document.getElementById("button1").style.display = "none";
}
}
document.getElementById("button1").onclick = reduceSlot;
function clearLocalStorage(){
localStorage.clear();
}
</script>
<body>
<p id="slot">10</p>
<a href="javascript:reduceSlot(1)" id="button1">Deduct</a>
<button onclick="clearLocalStorage()">Clear All</button>
</body>
I followed this code in a website but it doesn't run. Most of the websites that I browsed were confusing. I need help.
Upvotes: 1
Views: 7778
Reputation: 17757
<button onclick="window.localStorage.clear();">Clear All</button>
This should work.
Upvotes: 2