Reputation: 216
I want to keep count of the number of times my button is clicked but the page is relaoded after each click,how can i keep the count?
Upvotes: 0
Views: 1811
Reputation: 467
Use HTML5 localStorage
:
function addClicks() {
var currentValue = localStorage.getItem('myClicks') ? parseInt(localStorage.getItem('myClicks')) : 0;
var newValue = currentValue + 1;
localStorage.setItem('myClicks', newValue);
}
Upvotes: 2
Reputation: 2713
You can use localstorage
LocalStorage can only store string values. You can use parseInt which converts a string into an integer. It takes (key, value) pairs.
To set value
localstorage.setItem("count", "1");
and for each click, increment it like below
localstorage.setItem(parseInt(localStorage.getItem('count')) + 1);
Upvotes: 0
Reputation: 419
On each click of button save the counter value in cookie or local-storage via js or php for first time. There on just increment the counter by accessing cookie or local storage value and update the same for next clicks.
Upvotes: 0
Reputation: 8067
Store it in localstorage:
localStorage.clicks = +(localStorage.clicks || 0) + 1;
Upvotes: 2
Reputation: 31
You can keep the count in the cookie and always read and increment the count in the cookie. The other option could be that if your page is doing a post back then you can store it in a hidden field and then read and increment from the hidden field.
Let me know if you need help in coding. Happy to help :)
Upvotes: 0