Storage for button clicks












0















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">












share|improve this question


















  • 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
















0















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">












share|improve this question


















  • 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














0












0








0


1






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">












share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 10:07









Omer GilboaOmer Gilboa

155




155








  • 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














  • 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








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












3 Answers
3






active

oldest

votes


















1














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:
enter image description here






share|improve this answer

































    1














    We can use any one of these
    local storage,
    session storage,
    cookies,
    querystring,






    share|improve this answer































      0














      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");





      share|improve this answer
























        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
        });


        }
        });














        draft saved

        draft discarded


















        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









        1














        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:
        enter image description here






        share|improve this answer






























          1














          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:
          enter image description here






          share|improve this answer




























            1












            1








            1







            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:
            enter image description here






            share|improve this answer















            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:
            enter image description here







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 16 '18 at 15:24

























            answered Nov 16 '18 at 10:40









            SarahSarah

            1,1321025




            1,1321025

























                1














                We can use any one of these
                local storage,
                session storage,
                cookies,
                querystring,






                share|improve this answer




























                  1














                  We can use any one of these
                  local storage,
                  session storage,
                  cookies,
                  querystring,






                  share|improve this answer


























                    1












                    1








                    1







                    We can use any one of these
                    local storage,
                    session storage,
                    cookies,
                    querystring,






                    share|improve this answer













                    We can use any one of these
                    local storage,
                    session storage,
                    cookies,
                    querystring,







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 16 '18 at 10:15









                    TauqeerTauqeer

                    91




                    91























                        0














                        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");





                        share|improve this answer




























                          0














                          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");





                          share|improve this answer


























                            0












                            0








                            0







                            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");





                            share|improve this answer













                            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");






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 16 '18 at 10:22









                            JbadmintonJbadminton

                            60921031




                            60921031






























                                draft saved

                                draft discarded




















































                                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.




                                draft saved


                                draft discarded














                                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





















































                                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







                                Popular posts from this blog

                                The Sandy Post

                                Danny Elfman

                                Pages that link to "Head v. Amoskeag Manufacturing Co."