Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-09-19
3
Medium Priority
?
678 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
[X]
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
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
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…
Suggested Courses

636 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