Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Feb 2011
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts

    localStorage storing value as 11111?

    Hi all!

    In the code I am writing, when an image is clicked it sends a particular value to the function "redClick" which is in javascript.

    I am then trying to get the localStorage value for the key scoreRed. I am then clearing it, and then increasing the score by 1 and then storing it locally again.

    For some reason, I am not able to clear it using the localStorage.removeItem code, and when it does increment it, it appends a 1 to the score rather than increasing it. i.e. the score ends up being 11111 rather than 5.

    The code I have is below:

    Code:
    function redClick( buttonLocation ) {
    		if(buttonLocation == redLocation){
    			var redScore = localStorage.getItem("scoreRed"); //this is retrieving the score (scoreRed) from a previous game: lets say 5 and assigning it to redScore
    			localStorage.removeItem("scoreRed");
    			redScore = redScore +1; //this is incrementing the score, so now redScore = 6
    			localStorage.setItem("scoreRed", redScore); //this is saving the score - 6 - to a localstorage var called scoreRed
    			//window.location.reload();
    			}
    		}
    Any and all help would be much appreciated.

  • #2
    New Coder
    Join Date
    Feb 2011
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Just got some of the code to work. The new code is below:

    Code:
    		function redClick( buttonLocation ) {
    		if(buttonLocation == redLocation){
    			var redScore = localStorage.getItem("scoreRed");
    			 //this is retrieving the score (scoreRed) from a previous game: lets say 5 and assigning it to redScore
    			redScore = (redScore*1) +1; //this is incrementing the score, so now redScore = 6
    			localStorage.setItem("scoreRed", redScore); //this is saving the score - 6 - to a localstorage var called scoreRed
                            localStorage.removeItem("scoreRed");
    			}
    		}
    I cant seem to get the removeItem part of the code to work. Any ideas?

  • #3
    Regular Coder
    Join Date
    Dec 2010
    Location
    California
    Posts
    201
    Thanks
    30
    Thanked 8 Times in 8 Posts
    I would re-post this under Javascript as it has nothing to do with HTML and/or CSS!!

  • #4
    New Coder
    Join Date
    Feb 2011
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Actually localStorage is a HTML5 feature, hence I was posting it in this forum.

    Either way, I solved it myself. Thanks!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •