Solved

How do I add an IF within a javascript do-while loop?

Posted on 2011-09-21
3
154 Views
Last Modified: 2012-05-12
I have a do-while loop that loops for the maximum value of a localStorage variable. Each iteration creates a specific <li> statement. That logic works fine. I want to include an IF statement that will only allow the <li> statement to be created IF a specific condition of the localStorage variable is met.

If localStorage.[item + i] = "X" then I do NOT want to create the <li> statement. I simply want to increment the variable i and continue the do-while loop.

Here is the code that I am working with:

<ul>
   <script type="text/javascript">
   var i=1;
   do
   if localStorage["item" + i] = "X"
   {
    i++;
   }
   else
   {
    document.write("<li><a href='vcitemdetail.php?item="
       + localStorage["item" + i]
       + "&lsitag=" + "itag" + i
       + "&lsitem=" + "item" + i
       + "&lsdesc=" + "desc" + i
       + "'>"
       + localStorage["item" + i] + "-"
       + localStorage["desc" + i] +"</a></li>");
       i++;
   }       
 }
 while (i<=(parseInt(localStorage.trxCtr)));                        
</script>
</ul>
0
Comment
Question by:kbios
  • 2
3 Comments
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 total points
Comment Utility
check this code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
	<script>
		var val = parseInt(localStorage.trxCtr);
		window.onload = function(){
			var html = "";
			for( var i = 1; i < val; i++)
			{
				if ( localStorage["item" + i] != "X" )
				{
					html += "<li><a href='vcitemdetail.php?item="
				   + localStorage["item" + i]
				   + "&lsitag=" + "itag" + i
				   + "&lsitem=" + "item" + i
				   + "&lsdesc=" + "desc" + i
				   + "'>"
				   + localStorage["item" + i] + "-"
				   + localStorage["desc" + i] +"</a></li>";
			   }       
			}
			document.getElementById("list").innerHTML = html;
		};
	</script>
</HEAD>

<BODY>
	<ul id="list">
	</ul>
</BODY>
</HTML>

Open in new window

0
 

Author Closing Comment

by:kbios
Comment Utility
Thanks. The solution you provided worked. I could have used you yesterday. After many hours of testing and threads with EE I abondoned the for loop. We couldn't get it to work. We were trying to use it in the BODY not the HEAD but couldn't get it to work.

Final thought. No doubt your code is more efficient BUT is there a way to nest the IF within the do..while?
0
 
LVL 40

Expert Comment

by:gurvinder372
Comment Utility
check this one


var val = parseInt(localStorage.trxCtr);
window.onload = function(){
	var html = "";
	var i = 1; 
	do
	{
		if ( localStorage["item" + i] != "X" )
		{
			html += "<li><a href='vcitemdetail.php?item="
		   + localStorage["item" + i]
		   + "&lsitag=" + "itag" + i
		   + "&lsitem=" + "item" + i
		   + "&lsdesc=" + "desc" + i
		   + "'>"
		   + localStorage["item" + i] + "-"
		   + localStorage["desc" + i] +"</a></li>";
		}       
		i++;
	}while(i < val);
	document.getElementById("list").innerHTML = html;
};

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
In this tutorial viewers will learn how to define a gradient in CSS. Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Gradient. Define the background as "linear-gradient(to right, #ee3668, black)". Ensure you …
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…

763 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

12 Experts available now in Live!

Get 1:1 Help Now