Solved

How do I use javascript to create HTML <ul><li> tags?

Posted on 2011-09-20
12
404 Views
Last Modified: 2012-05-12
I am trying to use javascript to create a page with <ul><li> tags. I have some localStorage variables that I want to retreive and display as an unnumbered list on an HTML page.

localStorage.ctr = 3
localStorage.item1 = A
localStorage.item2 = B
localStorage.item3 = C

Here is the approach I'm working with:


<ul>

<script type="text/javascript"> {
var i = 0;
for (i=0; i<=Number(localStorage.ctr);i++);
   {
    document.write "<li><a href=x.php?item=localStorage["item" +i]></a></li>"
   }
}
</script>

</ul>

I'm expecting the following html code:

<ul>
  <li><a href=x.php?item=A></a></li>
  <li><a href=x.php?item=B></a></li>
  <li><a href=x.php?item=C></a></li>
</ul>

Any help/suggestions are appreciated.

0
Comment
Question by:kbios
  • 6
  • 4
  • 2
12 Comments
 

Author Comment

by:kbios
ID: 36569487
note: i would be initialize as 1 not 0. please review my syntax and offer suggestions. thanks
0
 
LVL 22

Expert Comment

by:Kim Walker
ID: 36569827
I'm not sure why you would need to create a Number object for localStorage.ctr unless there's a possibility it is not a number in which case your loop would fail. If you prefer to use a number object, you could use:

n=new Number(localStorage.ctr);
for (i=0; i<=n.valueOf();i++);

Open in new window


Personally, I would just parse the number as an integer:

for (i=0; i<=parseInt(localStorage.ctr);i++);

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 36569983
for example : http://jsfiddle.net/88eJ2/


<ul>
    <script language="javascript" type="text/javascript">
    localStorage = {};
    localStorage.ctr = 3
    localStorage.item1 = "A";
    localStorage.item2 = "B";
    localStorage.item3 = "C";
    for (i=1;i<=localStorage.ctr;i++)
    { 
        document.write("<li><a href='x.php?item=" + localStorage["item" +i] + "'>" + localStorage["item" +i] + "</a></li>");
    }

</script>
</ul>

Open in new window

0
 

Author Comment

by:kbios
ID: 36570016
I parsed the number as an integer as you suggested. I am still trying to get the <li> portion of the javascript to display properly on the HTML page. The following code should initially display 3 <li> detailed tags. At this point I'm trying to get the <li> to display. I will then come back use a variable for the item. Please look at the code and explain why the <li> tags are not displaying.

<script>
  window.onload = viewlist();
</script>
            
<div id="content">
  <div id="listitem">
    <ul>
      <script type="text/javascript" language="javascript">
         function viewlist()
         {
            var i =1;
            for (i=1; i<=parseInt(localStorage.trxCtr);i++);
            {
            document.write("<li><a href=getitemdetail.php?item='4004'></a></li>");
            }  
         }
       </script>
   </ul>
0
 

Author Comment

by:kbios
ID: 36570121
My localStorage variables should be available throughout my HTML I don't think I need/want the following contained within my function:
    localStorage = {};
    localStorage.ctr = 3
    localStorage.item1 = "A";
    localStorage.item2 = "B";
    localStorage.item3 = "C";

When I view the source the of the html page I see the text 'document.write' where I think I should only see the <li> portion. In php I don't see the word 'echo' I only see the code I want.

The for loop is not creating multiple lines as it should nor are those lines being displayed.
0
 

Author Comment

by:kbios
ID: 36570215
I don't think that my javascript function is getting called. I am using the following to launch the function.

<script>
  window.onload = viewlist();
</script>

Any other suggestions to automatically run the function. I want the function to execute on loading/entering the page without having to submit.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 22

Accepted Solution

by:
Kim Walker earned 500 total points
ID: 36570372
The execution of the function is not relevant to it's location in the html stream. I would expect, if the function were being executed, it would wipe out your entire page and replace it with the <li.....> code. document.write should only occur during page load. When you execute a document.write after the page load, it overwrites the page.

With that said, keep the script simple and let it run during the page load. You can leave your localStorage object in the head. It, too, should be outside any functions or loops in order to be global. In your comment above, you've used "localStorage.trxCtr" but I thought it was "localStorage.ctr".

<div id="content">
  <div id="listitem">
    <ul>
      < script type="text/ecmascript">
             var i =1;
            for (i=1; i<=parseInt(localStorage.ctr);i++);
            {
            document.write("<li><a href=getitemdetail.php?item='4004'>something here</a></li>");
            }  
       </script>
   </ul>

Open in new window


I need to point out that your sample does not include any text within the anchor tag. I've inserted "something here" in my code above to represent where the anchor text should appear. Otherwise, there's nothing for the view to click on to navigate to the linked page.

I've removed the language attribute of the script element. That attribute was deprecated some time ago. I've also used ecmascript instead of javascript. It really doesn't matter which you use there.

"ecmascript" is the politically correct name for javascript since it has no relationship to Java.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36570414
<html><head></head><body>
<ul>
    <script language="javascript" type="text/javascript">
    localStorage = {};
    localStorage.ctr = 3
    localStorage.item1 = "A";
    localStorage.item2 = "B";
    localStorage.item3 = "C";
    for (i=1;i<=localStorage.ctr;i++)
    { 
        document.write("<li><a href='x.php?item=" + localStorage["item" +i] + "'>" + localStorage["item" +i] + "</a></li>");
    }
</script>
</ul>
</body></html>

Open in new window

0
 

Author Comment

by:kbios
ID: 36570547
Here is the current code.

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

Thanks for the input on the anchor tag, it was inadvertantly deleted. I see where the code is now just a script and not a javascript and a big thanks for pointing toward the correct method. The <li> component DOES now appear. However I am expecting the number of <li>'s to be the value of my ctr. In the above example the counter is = 2 but only 1 <li> is displayed. Any thoughts on why the loop only returns 1 of the <li>'s?
0
 
LVL 22

Expert Comment

by:Kim Walker
ID: 36570548
I just noticed a flaw in your link. Change:

<a href=getitemdetail.php?item='4004'>

Open in new window


to:

<a href='getitemdetail.php?item=4004'>

Open in new window


(move the quote to follow the equals mark)
0
 

Author Closing Comment

by:kbios
ID: 36570722
Thanks for your help. I am closing this question but opening another. You answered this question but I now need the proper syntax to select the item from within localStorage.
0
 
LVL 22

Expert Comment

by:Kim Walker
ID: 36573339
This is what I get for copying and pasting and not double-checking my work. Remove the semi-colon at the end of the "for" declaration. You have:

     for (i=1;i<=parseInt(localStorage.trxCtr);i++);
      {
        document.write("<li><a href=getitemdetail.php?item='4004'>something here</a></li>");
      }

Open in new window


It should be:

     for (i=1;i<=parseInt(localStorage.trxCtr);i++)
      {
        document.write("<li><a href=getitemdetail.php?item='4004'>something here</a></li>");
      }

Open in new window


(no semi-colon at the end of line 1)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

863 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

25 Experts available now in Live!

Get 1:1 Help Now