?
Solved

What is the syntax of including a variable in document.write statement?

Posted on 2011-09-20
8
Medium Priority
?
269 Views
Last Modified: 2012-05-12
I have several localStorage variables set. There names and values are as follows:

localStorage.trxCtr = 2
localStorage.item1 = 1000
localStorage.item2 = 2000

Below is the code that I am currently trying to use. I want to loop for the value of my counter and create an <li> with the corresponding localStorage item.

<ul>
   <script type="text/ecmascript">
   var i =1;
   for (i=1;i<=parseInt(localStorage.trxCtr);i++);
   {
       document.write("<li><a href=getitemdetail.php?item="localStorage["item" + i ]">something here</a></li>");
   }  
  </script>
</ul>

I need the syntax to set the value of item in my <a href> statement.
0
Comment
Question by:kbios
  • 5
  • 3
8 Comments
 
LVL 83

Expert Comment

by:leakim971
ID: 36570822
Test page : http://jsfiddle.net/88eJ2/1/
<ul>
    <script language="javascript" type="text/javascript">
    localStorage = {};

localStorage.trxCtr = 2
localStorage.item1 = 1000
localStorage.item2 = 2000
    var i = 0;
    for (i=1;i<=localStorage.trxCtr;i++)
    { 
        document.write("<li><a href='getitemdetail.php?item=" + localStorage["item" +i] + "'>" + localStorage["item" +i] + "</a></li>");
    }

</script>
</ul>

Open in new window

0
 

Author Comment

by:kbios
ID: 36570849
In my code I do not have it as a javascript. According to a previous GURU there is no javascript needed so I'm using the regular script (ecmascript). I would prefer to use this due to some previous display issues I was having when using javascript.

The localStorage values are set in the client side localStorage area they do not need to be set in the javascript you supplied. Please look at this as not a javascript but as a ecmascript. How can I use the localStorage values within the <a href> tag?
0
 
LVL 83

Accepted Solution

by:
leakim971 earned 1500 total points
ID: 36570859
emacscript tag : http://jsfiddle.net/88eJ2/2/

http://en.wikipedia.org/wiki/ECMAScript

>The localStorage values are set in the client side localStorage area

no difference, did you try it?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:kbios
ID: 36571012
This HTML program IS running on the client side. I'm capturing data to the localStorage variables through a process on the client side. Once I have the localStorage variables I want to loop through the localStorage and retreive the data values and present them in an unnumbered list <ul><li><a href........> for each item. Then I'll select the item from the list which will call a php program and pass the item number to it.
0
 

Author Comment

by:kbios
ID: 36571020
yes I ran the code. the <li> shows up undefined. it's not retrieving the value from localStorage.
0
 

Author Comment

by:kbios
ID: 36571325
I was FINALLY able to get this code to work:

<ul>
  <script type="text/javascript">
  var i=1;
  do
  {
   document.write("<li><a href='getitemdetail.php?item=" + localStorage["item" + i] +"'>" + localStorage["item" + i] +"</a></li>");
   document.write("<br />");
   i++;
  }
 while (i<=(parseInt(localStorage.trxCtr)));                        
 </script>
</ul>

I have 1 final question before I accept the solution. I tried the above code using the for loop and the while loop and NEITHER one worked. I tried the do...while loop (above code) and it works great!!! Any insight as to why the other loops didn't work. Both the for and the while would not display each of the localStorage records ONLY the last record. Any insight would be GREATLY appreciated.

Thanks
0
 
LVL 83

Expert Comment

by:leakim971
ID: 36572191
The other loop use i<=
Must be : i<
0
 

Author Closing Comment

by:kbios
ID: 36572953
Actually both loops are set to i<= neither one of them are or were i<. Even if it were i< there still should have been multiple <li> being displayed. In this case the bottom line was that neither the for or while loops worked. This code only worked with the do...while.

I appreciate your effort in trying to help me solve this problem. Thanks.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
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…
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…
Suggested Courses

749 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