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

    Join Date
    Sep 2006
    Rep Power

    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.

    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
    USA (verifiably)
    Rep Power
    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.

    Learn CSS. | PHP includes | HTML Validator | CSS validator

    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