For Loop in javascript not working properly

Posted on 2008-10-27
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.
Question by:maryj152
LVL 16

Accepted Solution

sh0e earned 250 total points
ID: 22811680

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">


<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />


<!--<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] = "";

links[1] = "";

links[2] = "";

links[3] = "";

var includeit = new Array(4);

includeit[0] = "yes";

includeit[1] = "yes";

includeit[2] = "no";

includeit[3] = "no";

function showpubs(incl)

	{var ncl = incl;


	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>"





<style type="text/css">





<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" />




Open in new window


Expert Comment

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.

Author Closing Comment

ID: 31510255
Thank you for your prompt response

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

760 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

19 Experts available now in Live!

Get 1:1 Help Now