Reputation: 13
I am new to javascript and am currently working on a simple counter, the idea is that you will be able to store your counts in localStorage and clear them when you like.
I have been building on the W3C example but have been unable to clear the localStorage.
I have tried setting the value to zero as well as simply clearing and removing the item, but here is where i am at so far:
function clickCounter()
{
if(typeof(Storage)!=="undefined")
{
if (localStorage.clickcount)
{
localStorage.clickcount=Number(localStorage.clickcount)+1;
}
else
{
localStorage.clickcount=1;
}
document.getElementById("result").innerHTML="You have clicked the button " + localStorage.clickcount + " time(s).";
}
}
function cleanUp(){
localStorage.clickcount=0;
}
</script>
</head>
<body>
<br/>
<p><button onclick="clickCounter()" type="button">Click me!</button></p>
<button onclick="cleanUp()">Reset button</button>
<div id="result"></div>
Upvotes: 1
Views: 2152
Reputation: 85
If your are using js. you can use this, if you want to clear all data from localStorage. localStorage.clear();
But if you only want to clear a single localStorage, use localStorage.removeItem('key');
Ex.
function cleanUp(){
localStorage.clear(); //This clears all localStorage, when you click the button with this function
}
Upvotes: 0
Reputation: 5067
I copy pasted your code into an empty HTML file and ran it, works fine
All I did was add the missing < script >
I'm using Chrome 22
FYI this is how you remove something from local storage
localStorage.removeItem('thekey');
Upvotes: 1