Solved

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

Posted on 2011-09-21
3
155 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

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

896 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

11 Experts available now in Live!

Get 1:1 Help Now