Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2011-09-19
3
622 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
ID: 36562985
use : localStorage["item" + tctr ] = "VALUE";
0
 

Author Closing Comment

by:kbios
ID: 36563010
Thanks.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36563014
you're welcome, have a nice week!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

861 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