Solved

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

Posted on 2011-09-21
3
157 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Find out what you should include to make the best professional email signature for your organization.
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 position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…

762 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