#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    660
    Rep Power
    32

    Can a variable be used for the key in local storage setItem?


    I am trying to use a variable when storing into local storage because I have a list of values stored in an array, but it does not work.

    eg.
    Code:
    for(var i = 0; i<3; i++){
    var myName = nameList[i];
    var regName = name[i];
    localStorage.setItem(myName, regName);
    }
    I have tried converting myName into a string by
    String(myName) or myName.toString() and even used quotes in the setItem but it does not work!

    Is this possible??
  2. #2
  3. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,127
    Rep Power
    4304
    Being an "HTML5" feature does not necessarily make it an HTML question. This is a JavaScript question. *Thread Moved*

    I recommend you use the DOM tab of the Firebug extension in Firefox to check what is actually stored in localStorage.
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).

IMN logo majestic logo threadwatch logo seochat tools logo