_Unique_ Posted May 10, 2015 Share Posted May 10, 2015 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 Quote Link to comment https://forums.phpfreaks.com/topic/296179-adding-to-a-variable-stored-in-localstorage/ Share on other sites More sharing options...
_Unique_ Posted May 10, 2015 Author Share Posted May 10, 2015 Okay, for some reason this post has been made 3 times.. I have reported it to a moderator. Quote Link to comment https://forums.phpfreaks.com/topic/296179-adding-to-a-variable-stored-in-localstorage/#findComment-1511325 Share on other sites More sharing options...
Muddy_Funster Posted May 11, 2015 Share Posted May 11, 2015 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 Quote Link to comment https://forums.phpfreaks.com/topic/296179-adding-to-a-variable-stored-in-localstorage/#findComment-1511378 Share on other sites More sharing options...
maxxd Posted May 11, 2015 Share Posted May 11, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/296179-adding-to-a-variable-stored-in-localstorage/#findComment-1511418 Share on other sites More sharing options...
_Unique_ Posted May 12, 2015 Author Share Posted May 12, 2015 (edited) 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 May 12, 2015 by _Unique_ Quote Link to comment https://forums.phpfreaks.com/topic/296179-adding-to-a-variable-stored-in-localstorage/#findComment-1511609 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.