array of arrays - assigning values to variables inside array !

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hash4sp
    New Member
    • Aug 2007
    • 55

    #1

    array of arrays - assigning values to variables inside array !

    Hi,

    Can anyone please advise me on the following. Here I have a bunch of variables in an array. And then those arrays in another array. I want to assign values to those variables in arrays. Is this the correct way to do it?

    Code:
    <html>
    <body>
    
    <script type="text/javascript">
    
    var a,b,c,d,e,f;
    var set1=[a,b,c];
    var set2=[d,e,f];
    var comboAll=[set1,set2];
    
    function initializeVars(){
    	
    	for (var i=0; i<comboAll.length;i++){
    			
    		for (var j=0; j<comboAll[i].length;j++){
    			comboAll[i][j] = 'Hello ' + j ;
    		}	
    	}	
    
    	document.write('Start wrting variables<br />');	
    	document.write(a + '<br />');
    	document.write(b + '<br />');
    	document.write(c + '<br />');
    	document.write(d + '<br />');
    	document.write(e + '<br />');
    	document.write(f + '<br />');
    
    	
    }
    
    </script>
    
    </body>
    <input type='button' value='Initilize' onClick='initializeVars();' />
    </html>
    Best regards
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    first, don’t use document.write( ) after the page finished loading.

    Comment

    • hash4sp
      New Member
      • Aug 2007
      • 55

      #3
      @Dormilich
      I changed it with a DIV tag. Still doesnt work.
      It returns undefined for each variable.

      Code:
      <div id='results'></div>
      
      document.getElementById('results').innerHTML = a + ' ' + b + ' ' + c + ' ' + d + ' ' + e + ' ' + f;
      regards
      Last edited by hash4sp; Aug 24 '10, 08:36 AM. Reason: update

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        of course, you never set them anywhere.

        Comment

        • hash4sp
          New Member
          • Aug 2007
          • 55

          #5
          Thanks for you reply. I am setting the values for the array variables in the loop. Am i doing something wrong here.

          Code:
          <html>
          <body>
           
          <script type="text/javascript">
           
          var a,b,c,d,e,f;
          var set1=[a,b,c];
          var set2=[d,e,f];
          var comboAll=[set1,set2];
           
          function initializeVars(){
           
              for (var i=0; i<comboAll.length;i++){
           
                  for (var j=0; j<comboAll[i].length;j++){
                      comboAll[i][j] = 'Hello ' + j ;
                  }    
              }    
           
          document.getElementById('results').innerHTML = a + ' ' + b + ' ' + c + ' ' + d + ' ' + e + ' ' + f;
           
           
           
          }
           
          </script>
           
          </body>
          <input type='button' value='Initilize' onClick='initializeVars();' />
          <div id='results'></div>
          </html>

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            you don’t. what you’re setting is the comboAll variable, not a to f.

            Comment

            • hash4sp
              New Member
              • Aug 2007
              • 55

              #7
              @Dormilich

              Indeed thanks for your answers.
              Then how do I assign values during the loop to those variables from a to f?

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                you cannot. you would have to assign a to f manually.

                you should better think of a way, to tweak the output (admittedly, you have a very abstract example)

                Comment

                Working...