Solved

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

Posted on 2011-09-21
3
156 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
ID: 36575124
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
ID: 36575459
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
ID: 36575484
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

770 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