kbios
asked on
How do I use javascript to create HTML <ul><li> tags?
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=localStora ge["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.
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
{
document.write "<li><a href=x.php?item=localStora
}
}
</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.
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:
Personally, I would just parse the number as an integer:
n=new Number(localStorage.ctr);
for (i=0; i<=n.valueOf();i++);
Personally, I would just parse the number as an integer:
for (i=0; i<=parseInt(localStorage.ctr);i++);
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>
ASKER
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.t rxCtr);i++ );
{
document.write("<li><a href=getitemdetail.php?ite m='4004'>< /a></li>") ;
}
}
</script>
</ul>
<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.t
{
document.write("<li><a href=getitemdetail.php?ite
}
}
</script>
</ul>
ASKER
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.
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.
ASKER
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.
<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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
<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>
ASKER
Here is the current code.
<div id="itemlist">
<ul>
<script type="text/ecmascript">
var i =1;
for (i=1;i<=parseInt(localStor age.trxCtr );i++);
{
document.write("<li><a href=getitemdetail.php?ite m='4004'>s omething 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?
<div id="itemlist">
<ul>
<script type="text/ecmascript">
var i =1;
for (i=1;i<=parseInt(localStor
{
document.write("<li><a href=getitemdetail.php?ite
}
</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?
I just noticed a flaw in your link. Change:
to:
(move the quote to follow the equals mark)
<a href=getitemdetail.php?item='4004'>
to:
<a href='getitemdetail.php?item=4004'>
(move the quote to follow the equals mark)
ASKER
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.
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:
It should be:
(no semi-colon at the end of line 1)
for (i=1;i<=parseInt(localStorage.trxCtr);i++);
{
document.write("<li><a href=getitemdetail.php?item='4004'>something here</a></li>");
}
It should be:
for (i=1;i<=parseInt(localStorage.trxCtr);i++)
{
document.write("<li><a href=getitemdetail.php?item='4004'>something here</a></li>");
}
(no semi-colon at the end of line 1)
ASKER