• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 693
  • Last Modified:

How do I pass a variable as the HTML localStorage key value?

I want to create a dynamic key name for my localStorage variable within my application based on an incremented numeric value. I want to keep track of a numeric counter and then append the numeric value to a string, and the combined concatenation becomes the localStorage key.

For example:
tctr = 1      I want localStorage.item1 = "value";
tctr = 20    I want localStorage.item20 = "value";

Here is the javascript that I am using to increment the counter variable. I need some help with the variablization (sp?) of the key:

<script type="text/javascript">
    function addCart() {
    localStorage.trxCtr=Number(localStorage.trxCtr) +1;
    var tctr = Number(localStorage.trxCtr) + 1;
    localStorage.item(tctr)="VALUE";
    $('body').append('<div id="progress">Cart Updated</div>');
    $('#rightbutton').remove();
    <!-- history.go(-1); -->
    return true;                   
    }
</script>
0
kbios
Asked:
kbios
  • 2
1 Solution
 
leakim971PluritechnicianCommented:
use : localStorage["item" + tctr ] = "VALUE";
0
 
kbiosAuthor Commented:
Thanks.
0
 
leakim971PluritechnicianCommented:
you're welcome, have a nice week!
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now