Arrays – Insert into a div

Javascript arrays are just like any other object orientated language, in the aspect an array is a block of memory associated with main object. To create an array in Javascript just need to call
= Array(5);
= Array(‘value1′,’value2’);

The code below is using a default screen from which you press the ‘Click Here’ button and the array is used to insert into a <div> html tag. To insert into a div tag within javascript, there needs to be a new node created and in this instance a text node is created which in turn is appended to the div.

The code hopefully will explain more.

       <script language="javascript">
              // setup the main array
              var setArray = Array("hi", "there", "this", "is", "a","test");
              // insert the array into the DIV smalltest object
              function insertArray()
                     var theText = "";  // set the theText output to an empty string otherwise it would start with null.
                     for (var i=0; i < setArray.length; i++)
                            // create the array of text to insert
                            theText += setArray[i];
                     // create the createTextNode 
                     var insertText = document.createTextNode(theText);
              Just a small test, 
              <div id="smalltest">
              <input type="submit" value="click here" onclick="javascript:insertArray()" />

Leave a Reply

Your email address will not be published. Required fields are marked *