Storage for button clicks
I have a counter for button clicks, but if I refresh the page it sets back to '0'. How can I storage the input value so if I close the browser the previous number will remain still?
$(function() {
$('.counter').click(function() {
$(this).val(parseInt($(this).val()) + 1);
});
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" class="counter" value="0">javascript jquery html
add a comment |
I have a counter for button clicks, but if I refresh the page it sets back to '0'. How can I storage the input value so if I close the browser the previous number will remain still?
$(function() {
$('.counter').click(function() {
$(this).val(parseInt($(this).val()) + 1);
});
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" class="counter" value="0">javascript jquery html
5
You could uselocalStorage
– Rory McCrossan
Nov 16 '18 at 10:08
you can use AJAX to store new value to database every time a button is pressed so that you can retrieve it any time you want.
– Ram Bhandari
Nov 16 '18 at 10:16
add a comment |
I have a counter for button clicks, but if I refresh the page it sets back to '0'. How can I storage the input value so if I close the browser the previous number will remain still?
$(function() {
$('.counter').click(function() {
$(this).val(parseInt($(this).val()) + 1);
});
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" class="counter" value="0">javascript jquery html
I have a counter for button clicks, but if I refresh the page it sets back to '0'. How can I storage the input value so if I close the browser the previous number will remain still?
$(function() {
$('.counter').click(function() {
$(this).val(parseInt($(this).val()) + 1);
});
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" class="counter" value="0">$(function() {
$('.counter').click(function() {
$(this).val(parseInt($(this).val()) + 1);
});
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" class="counter" value="0">$(function() {
$('.counter').click(function() {
$(this).val(parseInt($(this).val()) + 1);
});
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" class="counter" value="0">javascript jquery html
javascript jquery html
asked Nov 16 '18 at 10:07
Omer GilboaOmer Gilboa
155
155
5
You could uselocalStorage
– Rory McCrossan
Nov 16 '18 at 10:08
you can use AJAX to store new value to database every time a button is pressed so that you can retrieve it any time you want.
– Ram Bhandari
Nov 16 '18 at 10:16
add a comment |
5
You could uselocalStorage
– Rory McCrossan
Nov 16 '18 at 10:08
you can use AJAX to store new value to database every time a button is pressed so that you can retrieve it any time you want.
– Ram Bhandari
Nov 16 '18 at 10:16
5
5
You could use
localStorage– Rory McCrossan
Nov 16 '18 at 10:08
You could use
localStorage– Rory McCrossan
Nov 16 '18 at 10:08
you can use AJAX to store new value to database every time a button is pressed so that you can retrieve it any time you want.
– Ram Bhandari
Nov 16 '18 at 10:16
you can use AJAX to store new value to database every time a button is pressed so that you can retrieve it any time you want.
– Ram Bhandari
Nov 16 '18 at 10:16
add a comment |
3 Answers
3
active
oldest
votes
You could use local storage to store a key/value pair. Here is an example of how you could use it:
$(document).ready(function() {
//Firstly check if buttonClickCounter is in local storage from before.
if(localStorage.getItem("buttonClickCounter") === null){
//buttonClickCounter is not in local storage.
}else{
//buttonClickCounter is in local storage.
var counter = localStorage.getItem("buttonClickCounter");
//insert the counter value into the HTML.
$('.counter').val(counter);
}
$('.counter').click(function() {
var newValue = parseInt($(this).val()) + 1;
$(this).val(newValue);
//store the new counter value in local storage.
localStorage.setItem('buttonClickCounter', newValue);
});
});
You can check local storage values in the console under the Application tab as follows:

add a comment |
We can use any one of these
local storage,
session storage,
cookies,
querystring,
add a comment |
You can use localstorage or cookies for this.
Take a look at these websites:
Localstorage: Localstorage
Cookies: Cookies
Example localstorage:
var buttonvalue = $('.counter').val();
localStorage.setItem("buttonvalue", buttonvalue);
// get value of button
var someVarName = localStorage.getItem("buttonvalue");
For cookies in Jquery you should include a library:
https://github.com/carhartl/jquery-cookie
Then you can use this:
$.cookie("buttonvalue", $('.counter').val());
var cookieValue = $.cookie("buttonvalue");
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335564%2fstorage-for-button-clicks%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could use local storage to store a key/value pair. Here is an example of how you could use it:
$(document).ready(function() {
//Firstly check if buttonClickCounter is in local storage from before.
if(localStorage.getItem("buttonClickCounter") === null){
//buttonClickCounter is not in local storage.
}else{
//buttonClickCounter is in local storage.
var counter = localStorage.getItem("buttonClickCounter");
//insert the counter value into the HTML.
$('.counter').val(counter);
}
$('.counter').click(function() {
var newValue = parseInt($(this).val()) + 1;
$(this).val(newValue);
//store the new counter value in local storage.
localStorage.setItem('buttonClickCounter', newValue);
});
});
You can check local storage values in the console under the Application tab as follows:

add a comment |
You could use local storage to store a key/value pair. Here is an example of how you could use it:
$(document).ready(function() {
//Firstly check if buttonClickCounter is in local storage from before.
if(localStorage.getItem("buttonClickCounter") === null){
//buttonClickCounter is not in local storage.
}else{
//buttonClickCounter is in local storage.
var counter = localStorage.getItem("buttonClickCounter");
//insert the counter value into the HTML.
$('.counter').val(counter);
}
$('.counter').click(function() {
var newValue = parseInt($(this).val()) + 1;
$(this).val(newValue);
//store the new counter value in local storage.
localStorage.setItem('buttonClickCounter', newValue);
});
});
You can check local storage values in the console under the Application tab as follows:

add a comment |
You could use local storage to store a key/value pair. Here is an example of how you could use it:
$(document).ready(function() {
//Firstly check if buttonClickCounter is in local storage from before.
if(localStorage.getItem("buttonClickCounter") === null){
//buttonClickCounter is not in local storage.
}else{
//buttonClickCounter is in local storage.
var counter = localStorage.getItem("buttonClickCounter");
//insert the counter value into the HTML.
$('.counter').val(counter);
}
$('.counter').click(function() {
var newValue = parseInt($(this).val()) + 1;
$(this).val(newValue);
//store the new counter value in local storage.
localStorage.setItem('buttonClickCounter', newValue);
});
});
You can check local storage values in the console under the Application tab as follows:

You could use local storage to store a key/value pair. Here is an example of how you could use it:
$(document).ready(function() {
//Firstly check if buttonClickCounter is in local storage from before.
if(localStorage.getItem("buttonClickCounter") === null){
//buttonClickCounter is not in local storage.
}else{
//buttonClickCounter is in local storage.
var counter = localStorage.getItem("buttonClickCounter");
//insert the counter value into the HTML.
$('.counter').val(counter);
}
$('.counter').click(function() {
var newValue = parseInt($(this).val()) + 1;
$(this).val(newValue);
//store the new counter value in local storage.
localStorage.setItem('buttonClickCounter', newValue);
});
});
You can check local storage values in the console under the Application tab as follows:

edited Nov 16 '18 at 15:24
answered Nov 16 '18 at 10:40
SarahSarah
1,1321025
1,1321025
add a comment |
add a comment |
We can use any one of these
local storage,
session storage,
cookies,
querystring,
add a comment |
We can use any one of these
local storage,
session storage,
cookies,
querystring,
add a comment |
We can use any one of these
local storage,
session storage,
cookies,
querystring,
We can use any one of these
local storage,
session storage,
cookies,
querystring,
answered Nov 16 '18 at 10:15
TauqeerTauqeer
91
91
add a comment |
add a comment |
You can use localstorage or cookies for this.
Take a look at these websites:
Localstorage: Localstorage
Cookies: Cookies
Example localstorage:
var buttonvalue = $('.counter').val();
localStorage.setItem("buttonvalue", buttonvalue);
// get value of button
var someVarName = localStorage.getItem("buttonvalue");
For cookies in Jquery you should include a library:
https://github.com/carhartl/jquery-cookie
Then you can use this:
$.cookie("buttonvalue", $('.counter').val());
var cookieValue = $.cookie("buttonvalue");
add a comment |
You can use localstorage or cookies for this.
Take a look at these websites:
Localstorage: Localstorage
Cookies: Cookies
Example localstorage:
var buttonvalue = $('.counter').val();
localStorage.setItem("buttonvalue", buttonvalue);
// get value of button
var someVarName = localStorage.getItem("buttonvalue");
For cookies in Jquery you should include a library:
https://github.com/carhartl/jquery-cookie
Then you can use this:
$.cookie("buttonvalue", $('.counter').val());
var cookieValue = $.cookie("buttonvalue");
add a comment |
You can use localstorage or cookies for this.
Take a look at these websites:
Localstorage: Localstorage
Cookies: Cookies
Example localstorage:
var buttonvalue = $('.counter').val();
localStorage.setItem("buttonvalue", buttonvalue);
// get value of button
var someVarName = localStorage.getItem("buttonvalue");
For cookies in Jquery you should include a library:
https://github.com/carhartl/jquery-cookie
Then you can use this:
$.cookie("buttonvalue", $('.counter').val());
var cookieValue = $.cookie("buttonvalue");
You can use localstorage or cookies for this.
Take a look at these websites:
Localstorage: Localstorage
Cookies: Cookies
Example localstorage:
var buttonvalue = $('.counter').val();
localStorage.setItem("buttonvalue", buttonvalue);
// get value of button
var someVarName = localStorage.getItem("buttonvalue");
For cookies in Jquery you should include a library:
https://github.com/carhartl/jquery-cookie
Then you can use this:
$.cookie("buttonvalue", $('.counter').val());
var cookieValue = $.cookie("buttonvalue");
answered Nov 16 '18 at 10:22
JbadmintonJbadminton
60921031
60921031
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335564%2fstorage-for-button-clicks%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
5
You could use
localStorage– Rory McCrossan
Nov 16 '18 at 10:08
you can use AJAX to store new value to database every time a button is pressed so that you can retrieve it any time you want.
– Ram Bhandari
Nov 16 '18 at 10:16