using jquery to retain a variable set in button click event to be available after page refresh

i have a html page where i'm using jquery

In my jquery i have this code

        $(document).ready(function () {
            // -------------------

            // *********** On Page Load ***********
				
            // copy variable called testvariable1 to a variable called testvariable2
            var testvariable2 = testvariable1;
            
            
            // *********** On Page Load ***********

            
            // *********** button click event  ***********
            $('.Button1Class').click(function () {

                // on button click set variable called testvariable1 to 2
                var testvariable1 = 2;
                
            });
           // *********** button click event  ***********
            
            
            // -------------------
        });

Open in new window


in my button click event, on button click a variable called testvariable2 is set to 2.

I want to copy testvariable1 to another variable called testvariable2 in the pageload event.

But i noticed the above code doesn't work because the variable i set in the button click is lost when I do a page refresh.

I read that I could use localstorage.

so then if I change my button click event to this, then  testvariable2 would be available on page load.

            // *********** button click event  ***********
            $('.Button1Class').click(function () {

                // on button lick set variable called testvariable1 to 2
                var testvariable1 = 2;
               
                // copy variable testvariable1 to testvariable2 to localstorage for use on page load
                var testvariable2 = testvariable1;
                localStorage.setItem("testvariable2", testvariable2);

            });
           // *********** button click event  ***********


Is there better way to copy a variable from button click event to be available after a page reload?
LVL 1
maqskywalkerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
A page reload/refresh restores the page as it was originally loaded, at least for the HTML part.  'localStorage' looks like a good option if you are not already using a database to populate the page.

http://www.w3schools.com/html/html5_webstorage.asp

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.