Solved

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

Posted on 2011-09-20
8
246 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 82

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 82

Accepted Solution

by:
leakim971 earned 500 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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 82

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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
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…

830 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