We help IT Professionals succeed at work.
Get Started

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

maqskywalker
maqskywalker asked
on
134 Views
Last Modified: 2015-10-04
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?
Comment
Watch Question
Fixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014
Commented:
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE