0

I would like the var value to have the same value after refreshing, e.g if the button is pressed 5 times, then the user refreshes, the paragraph 'print' will show 6 instead of 1, which it is set to do currently.

Here is my code:

var value = 1;
$(document).ready(function() {
    $('#print').text(value);
});

$('#add1Button').click(function() {
    value = value + 1;
    $('#print').text(value);
});
2

1 Answer 1

4

If I understand you correctly, you need to have same value even after user refreshes the page? If that is correct, then you can:

  1. Write that value to cookie
  2. Use local storage
  3. Store the value on server and retrieve it when page is loaded.
  4. (Suggested by PHPglue, thanks): you can use Hash or get value in the url

First option is simplest and supported by all browsers. Local storage is HTML5 feature and you should check which browsers support it. Third option is most complex.

Sign up to request clarification or add additional context in comments.

3 Comments

Don't forget you could also add a hash or GET value to the URL.
@PHPglue, if you make that an answer I'll upvote it, I didn't think of that when I saw the question, good thinking.....
Second it, great idea with hash

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.