Solved

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

Posted on 2011-09-19
3
587 Views
Last Modified: 2012-06-27
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
Comment
Question by:kbios
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
use : localStorage["item" + tctr ] = "VALUE";
0
 

Author Closing Comment

by:kbios
Comment Utility
Thanks.
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
you're welcome, have a nice week!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now