Jump to content

Adding to a variable stored in localStorage


_Unique_

Recommended Posts

Hello,

I am attempting to create an install wizard, and when the 'next' button is pressed, the step variable that is stored in localStorage is added +1. So if the variable in localStorage was 1, I wanted to add 1 to the variable to become 2. However, so far what I have made is when the script is run, and the variable is added +1 to, but not how I would like it. At the moment, what it does is adds a 1 at the end of the variable, so if the variable was 1, the variable would become 11, and not 2.

 

This is my script so far;

<script>
    $(document).ready(function () {
        
        var currentStep = 0;
        
        if(localStorage.getItem('Step') === null) {
            localStorage.setItem('Step', currentStep);
        }
        
        $('#next').click(function() {
            currentStep = localStorage.getItem('Step');
            nextStep = currentStep + 1;
            localStorage.setItem('Step', nextStep);
            alert(localStorage.getItem('Step'));
        });
    });
</script>

I am fairly new to jQuery/Javascript, so it could be something real simple that I am missing.

 

Thanks in advance,

 

Unique

there are two options to try:

you could use the increment operator ++ instead of +1

 

nextStep = currentStep++; // as increment is exclusivly arithmetic it sould convert the currentStep variable into a number for you

or you could parseInt(currentStep) + 1

 

nextStep = parseInt(currentStep) + 1; // this manualy typecast's your variable into a number, meening that the +1 should behave as expected in this scenrio

In JavaScript, '+' is the concatenation operator. So, if your number is internally a string, ' + 1' will append the string '1' to the current value, thus turning '1' into '11'. As Muddy_Funster suggests, using parseInt() on the current value will make sure that JS sees the value as an integer and adding 1 will actually add 1 to the total.

In JavaScript, '+' is the concatenation operator. So, if your number is internally a string, ' + 1' will append the string '1' to the current value, thus turning '1' into '11'. As Muddy_Funster suggests, using parseInt() on the current value will make sure that JS sees the value as an integer and adding 1 will actually add 1 to the total.

 

 

 

there are two options to try:

you could use the increment operator ++ instead of +1

nextStep = currentStep++; // as increment is exclusivly arithmetic it sould convert the currentStep variable into a number for you

or you could parseInt(currentStep) + 1

nextStep = parseInt(currentStep) + 1; // this manualy typecast's your variable into a number, meening that the +1 should behave as expected in this scenrio

 

Yep, the parseInt() worked, thanks!

 

Oh, and for future references for myself or anyone else;

I decided to change from localStorage() to sessionStorage() because sessionStorage did not reset on refresh, but reset on tab/browser close, which is perfect for what I am creating, whereas localStorage cannot be cleared on only browser close, you can only set it to clear on refresh and tab/browser closed, unfortunately.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.