Solved

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

Posted on 2011-09-20
8
242 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:kbios
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

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

Author Comment

by:kbios
Comment Utility
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
Comment Utility
The other loop use i<=
Must be : i<
0
 

Author Closing Comment

by:kbios
Comment Utility
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

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

Title # Comments Views Activity
Accessing data in JSON response.d 4 24
Bootstrap 3 and Angular 2 12 19
SVG Logo 4 16
Apostophes in PHP generated form 6 11
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
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…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

728 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