Jump to content

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.

Edited by _Unique_
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.