Solved

For Loop in javascript not working properly

Posted on 2008-10-27
3
315 Views
Last Modified: 2011-10-19
Depending on the content of the relative index in array includeit, I want to output, using innerHTML, the correct content of array titles and array links.
The script only generates one line. Each selection should generate two lines.
Maybe it is just a typo but I can't see it.
buttontest.txt
0
Comment
Question by:maryj152
[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
3 Comments
 
LVL 16

Accepted Solution

by:
sh0e earned 250 total points
ID: 22811680

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>testnt</title>
<!--<script type="text/javascript" src="scripts/extn.js"> -->
<script type="text/javascript">
var titles = new Array (4);
titles[0] = "Publications Plus (ACES & Extension Publications)";
titles[1] = "ITCS Instructional Materials";
titles[2] = "Extension Turfgrass Publications   ";
titles[3] = "Illinois Steward  ";
 
var links = new Array(4);
links[0] = "https://pubsplus.uiuc.edu/";
links[1] = "http://im.itcs.uiuc.edu/";
links[2] = "http://www.turf.uiuc.edu/extension/extension.html";
links[3] = "http://web.extension.uiuc.edu/illinoissteward/";
 
var includeit = new Array(4);
includeit[0] = "yes";
includeit[1] = "yes";
includeit[2] = "no";
includeit[3] = "no";
 
function showpubs(incl)
	{var ncl = incl;
    document.getElementById("pubs").innerHTML="";
	for(i = 0; i< includeit.length; i++)
		{  if(includeit[i] == ncl)
		 {document.getElementById("pubs").innerHTML+="<a href="+links[i]+"><h3 class=blu>"+titles[i]+"</h3></a>"
			}
		}
	}
 
 
</script>
<style type="text/css">
.blu{color:blue;}
</style>
</head>
 
<body>
<div id="pubs">Content for id "pubs" Goes Here
  <input name="button" type="button" onclick="showpubs('yes')" value="yes" /><br />
<br />
<input name="button" type="button" onclick="showpubs('no')" value="no" />
</div>
</body>
</html>

Open in new window

0
 
LVL 8

Expert Comment

by:OBonio
ID: 22812017
In case you can't see your error, you want to use += when appending to innerHTML instead of just assigning the text with =.  = overwrites what is there already.
0
 

Author Closing Comment

by:maryj152
ID: 31510255
Thank you for your prompt response
0

Featured Post

Independent Software Vendors: 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

Title # Comments Views Activity
Link Anchor Not working 9 67
What kind of script/language created this graph? 6 67
JSON help 8 48
CSS Selector Chain NOT working for this situation? 3 16
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

759 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