<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dara Berry &#187; Tutorials</title>
	<atom:link href="http://www.daraberry.com/category/tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.daraberry.com</link>
	<description>personal blog of a web developer</description>
	<lastBuildDate>Tue, 20 Apr 2010 13:16:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Add JavaScript Generated Text to a Div with an Array and For Loop</title>
		<link>http://www.daraberry.com/tutorials/add-javascript-generated-text-to-a-div-with-an-array-and-for-loop/</link>
		<comments>http://www.daraberry.com/tutorials/add-javascript-generated-text-to-a-div-with-an-array-and-for-loop/#comments</comments>
		<pubDate>Sat, 14 Nov 2009 01:44:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[School Stuff]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[div]]></category>
		<category><![CDATA[for loop]]></category>

		<guid isPermaLink="false">http://www.daraberry.com/?p=68</guid>
		<description><![CDATA[We will create an array in JavaScript and use a for statement to output text into a div. ]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re here to learn about JavaScript, I am assuming you know basic HTML and some (minimal) programming experience. We are going to create an array in JavaScript and will use a for loop to output the generated text into a div.<span id="more-68"></span> We&#8217;re going to store the top 10 MMO&#8217;s in an array (<a href="http://www.gameogre.com/topmmorpgs.htm">source</a>):</p>
<ol>
<li>World of Warcraft</li>
<li>Lord of the Rings Online</li>
<li>Aion</li>
<li>Eve Online</li>
<li>Final Fantasy XI</li>
<li>City of Heros</li>
<li>Champions Online</li>
<li>Dark Age of Camelot</li>
<li>Warhammer Online</li>
<li>Age of Conan</li>
</ol>
<p>We are going to get more familiar with the following:</p>
<ul>
<li>JavaScript Arrays</li>
<li>For Loop</li>
<li>Functions</li>
<li>ID&#8217;s and Divs</li>
<li>ordered lists</li>
</ul>
<p>Ok, now on to the good stuff. Let&#8217;s create an array.</p>
<blockquote><p>If you&#8217;re not familiar with what an array is, think of it as a mini spreadsheet (at least I do). It holds variable data in more than one &#8216;cell&#8217;. It can hold a list of items (one dimensional) &#8211; imagine a row of data in a spreadsheet. It can also hold more than one row of data (multi-dimensional), just like a spreadsheet. We will be working with the simple, one dimensional array.</p></blockquote>
<pre class="brush: jscript;">
var games = new Array(10);
games[0] = &quot;World of Warcraft&quot;;
games[1] = &quot;Lord of the Rings Online&quot;;
games[2] = &quot;Aion&quot;;
games[3] = &quot;Eve Online&quot;;
games[4] = &quot;Final Fantasy XI&quot;;
games[5] = &quot;City of Heros&quot;;
games[6] = &quot;Champions Online&quot;;
games[7] = &quot;Dark Age of Camelot&quot;;
games[8] = &quot;Warhammer Online&quot;;
games[9] = &quot;Age of Conan&quot;;
</pre>
<p>I want to point out three things here.</p>
<ol>
<li>You notice the array starts at 0, not 1. Computers count from 0 naturally, so keep this in mind when you&#8217;re programming.</li>
<li>The number 10 in new Array(10) does not have to be there. You can put a number here if you want to limit how big the array gets. You might want to do this for data you get from an outside source.</li>
<li>There is more than one way to create this array. You can create the same array like this:</li>
</ol>
<pre class="brush: jscript;">var games = new Array(&quot;World of Warcraft&quot;, &quot;Lord of the Rings Online&quot;, &quot;Aion&quot;, &quot;Eve Online&quot;, &quot;Final Fantasy XI&quot;, &quot;City of Heros&quot;, &quot;Champions Online&quot;, &quot;Dark Age of Camelot&quot;, &quot;Warhammer Online&quot;, &quot;Age of Conan&quot;)</pre>
<p>The comma separates each item and each item is numbered automatically (remember it starts at 0).</p>
<p>Now we have the array, we need to create a function to start our for loop when our body content is loaded. Otherwise, you won&#8217;t see anything. Before we do that, let&#8217;s go ahead and create our body text and add the function call to our body tag.</p>
<pre class="brush: xml; highlight: [1,6];">&lt;body onload=&quot;pageLoaded()&quot;&gt;

 &lt;div style=&quot;width: 600px; margin: auto; padding: 15px; border: 1px solid gray; font-family: sans-serif;&quot;&gt;
 &lt;h1&gt;Top 10 MMO's&lt;/h1&gt;

 &lt;div id=&quot;result&quot;&gt;loading data &lt;/div&gt;
 &lt;/div&gt;

&lt;/body&gt;</pre>
<p>What we&#8217;re mostly concerned with is the div with the id #result. We&#8217;re going to use this to identify the div we want our text placed in. Ok, back to our JavaScript.</p>
<p>We are going to create our function:</p>
<pre class="brush: jscript;">function pageLoaded(){
//create variable to hold the text generated by the for loop
var list = &quot;&lt;ol&gt;&quot;;
}</pre>
<p>We create our variable &#8220;list&#8221; to hold the text that will be generated by our for loop. We are going to output our items in an ordered list, so we add the opening ordered list tag to the variable when we declare it. We don&#8217;t want this repeated each time the for loop runs, now do we?</p>
<p>The For Loop</p>
<p>Let&#8217;s break down what this for loop does, shall we?</p>
<pre class="brush: jscript;">// set parameters for array - games.length refers to the number of items in the array
 for(i=0; i&lt;games.length; i++){
 // adds a list item to the variable 'list' - we use += to add our strings
 list +=&quot;&lt;li&gt;&quot;+games[i]+&quot;&lt;/li&gt;&quot;;
 }//end for</pre>
<p>The for loop needs three parameters within it&#8217;s parenthesis. This is key to telling it how many time it&#8217;s going to repeat.</p>
<pre class="brush: jscript;">(i=0; i&lt;games.length; i++)</pre>
<p><strong>i=0</strong> &#8211; We created a variable called i, and set it&#8217;s value equal to 0 (because our array starts with 0).<strong> </strong></p>
<p><strong>i&lt;games.length</strong> &#8211; as long as i is less than the length of the games array (how many items are stored), it will run<strong> </strong></p>
<p><strong>i++</strong> after loop runs, add 1 to i</p>
<pre class="brush: jscript;">list +=&quot;&lt;li&gt;&quot;+games[i]+&quot;&lt;/li&gt;&quot;;</pre>
<p>Notice we are using += instead of just an =. If we just used an =, it would replace the old data with the new, instead of adding to it. Slick, huh?</p>
<pre class="brush: jscript;">&quot;&lt;li&gt;&quot;+games[i]+&quot;&lt;/li&gt;&quot;;</pre>
<p>We cycle though the array, adding a list item each time to our variable.</p>
<p>We&#8217;re done with our for loop but we have a few more things to add. We need to close off our ordered list.</p>
<pre class="brush: jscript; highlight: [9];">// created variable 'list' that will hold our ordered list
 var list = &quot;&lt;ol&gt;&quot;;
 // set parameters for array - games.length refers to the number of items in the array
 for(i=0; i&lt;games.length; i++){
 // adds a list item to the variable 'list' - we use += to add our strings
 list +=&quot;&lt;li&gt;&quot;+games[i]+&quot;&lt;/li&gt;&quot;;
 }//end for
 // since the loop is done, add the end tag to the ordered list
 list +=&quot;&lt;/ol&gt;&quot;;</pre>
<p>The last thing we&#8217;re going to do is add the contents of our variable into the #result div.</p>
<pre class="brush: jscript; highlight: [11];">// created variable 'list' that will hold our ordered list
 var list = &quot;&lt;ol&gt;&quot;;
 // set parameters for array - games.length refers to the number of items in the array
 for(i=0; i&lt;games.length; i++){
 // adds a list item to the variable 'list' - we use += to add our strings
 list +=&quot;&lt;li&gt;&quot;+games[i]+&quot;&lt;/li&gt;&quot;;
 }//end for
 // since the loop is done, add the end tag to the ordered list
 list +=&quot;&lt;/ol&gt;&quot;;
 // write the variable list contents (now filled with the array items) to the div #result
 document.getElementById(&quot;result&quot;).innerHTML=list;</pre>
<p><strong>document</strong>- this html page<strong> </strong></p>
<p><strong>getElementById(&#8221;result&#8221;)</strong> &#8211; find the element that has id=&#8221;result&#8221; tag in it. <em>Note that this can refer to something besides a div such as an image or a link, as well as other elements.</em></p>
<p><strong>innerHTML</strong> &#8211; while it isn&#8217;t 100% correct to use this, it works just fine in modern browsers as well as some older ones.</p>
<p><strong>=list</strong> &#8211; Long story short, we&#8217;re replacing the HTML code within the #result div to what we have stored in our variable. <em>Note that when we set the HTML code to our variable, it replaces any code or formatting you have within the div.</em></p>
<p>Here is what our finished code should look like:</p>
<p id="line1">
<pre class="brush: xml;">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
&lt;title&gt;Fun with JavaScript Stuffz&lt;/title&gt;

&lt;script type=&quot;text/javascript&quot; language=&quot;javascript&quot;&gt;
var games = new Array();
 games[0] = &quot;World of Warcraft&quot;;
 games[1] = &quot;Lord of the Rings Online&quot;;
 games[2] = &quot;Aion&quot;;
 games[3] = &quot;Eve Online&quot;;
 games[4] = &quot;Final Fantasy XI&quot;;
 games[5] = &quot;City of Heros&quot;;
 games[6] = &quot;Champions Online&quot;;
 games[7] = &quot;Dark Age of Camelot&quot;;
 games[8] = &quot;Warhammer Online&quot;;
 games[9] = &quot;Age of Conan&quot;;

function pageLoaded(){
 // created variable 'list' that will hold our ordered list
 var list = &quot;&lt;ol&gt;&quot;;
 // set parameters for array - games.length refers to the number of items in the array
 for(i=0; i&lt;games.length; i++){
 // adds a list item to the variable 'list' - we use += to add our strings
 list +=&quot;&lt;li&gt;&quot;+games[i]+&quot;&lt;/li&gt;&quot;;
 }//end for
 // since the loop is done, add the end tag to the ordered list
 list +=&quot;&lt;/ol&gt;&quot;;
 // write the variable list contents (now filled with the array items) to the div #result
 document.getElementById(&quot;result&quot;).innerHTML=list;
}//end pageLoaded Function

&lt;/script&gt;
&lt;/head&gt;

&lt;body onload=&quot;pageLoaded()&quot;&gt;

 &lt;div style=&quot;width: 600px; margin: auto; padding: 15px; border: 1px solid gray; font-family: sans-serif;&quot;&gt;
 &lt;h1&gt;Top 10 MMO's&lt;/h1&gt;
 &lt;div id=&quot;result&quot;&gt;loading data &lt;/div&gt;
 &lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>
<p><a href="http://www.daraberry.com/http://www.daraberry.com/wp-content/uploads/2009/11/javascript-text-to-div.html">View Example HTML Page</a></p>
<p>I hope you enjoyed this! <img src='http://www.daraberry.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.daraberry.com/tutorials/add-javascript-generated-text-to-a-div-with-an-array-and-for-loop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
