Hello i want to create a array in java script withing 2 for loops

var i;
    var a;
    var total		= document.getElementsByName('qm[7]')	
    var creativity	= document.getElementsByName('qm[0]');
    var design		= document.getElementsByName('qm[1]');
    var text		= document.getElementsByName('qm[3]');
    var motivation	= document.getElementsByName('qm[5]');
    var depth		= document.getElementsByName('qm[6]');
    var usefulness	= document.getElementsByName('qm[8]');
    var research	= document.getElementsByName('qm[9]');

ratingArray = new Array(total,creativity,design,text,motivation,depth,usefulness,research);

for(i=0; i < ratingArray.length;i++)
{

	for(a=0; a < ratingArray[i].length;a++)
	{
		if(ratingArray[i][a].checked == true)
		{

			 rateArray = new Array(ratingArray[i][a].value);
		}	 
	}

}

and if i return rateArray it just gives the first element any idea?

share|improve this question
1  
The script is doing what you want it to do. What do you want to create? A 1D array with all the checked items? – C. Ross Oct 21 '09 at 12:54

4 Answers

up vote 4 down vote accepted

You're overwriting rateArray each time you find a checked element - I suspect you meant to append it instead:

var ratingArray = new Array(total,creativity,design,text,motivation,depth,usefulness,research);
var rateArray = new Array();

for(i=0; i < ratingArray.length;i++)
{

        for(a=0; a < ratingArray[i].length;a++)
        {
                if(ratingArray[i][a].checked == true)
                {

                         rateArray.push(ratingArray[i][a].value);
                }        
        }

}
share|improve this answer
Thank you very much this worked fine for me have a nice day – streetparade Oct 21 '09 at 13:09

Create a new array and push the selected values to the new array.

A detailed description of array functions

Manipulating JavaScript Arrays

var ratingArray = new Array(total,creativity,design,text,motivation,depth,usefulness,research);

var selectedValArray = [];

for(i=0; i < ratingArray.length;i++)
{

        for(a=0; a < ratingArray[i].length;a++)
        {
                if(ratingArray[i][a].checked == true)
                {

                         selectedValArray.push ( ratingArray[i][a].value );
                }        
        }

}
share|improve this answer

The statement

document.getElementsByName('qm[7]')

will not work. There are no elements that can have the name qm[7]. Did you mean this to be your array? In that case, remove the quotes, initialize the array prior to those statements and fill it with the names of the elements you want to select.

The function getElementsByName returns an array of elements. To use this array, you need to select the items in it. I.e.:

var elems = document.getElementsByName("body");
var myBody = elems[0];

you do that correctly in your for-loops.

Update: expanded section and added explanation on getElementsByTagName

share|improve this answer

In this line you create an new Array every time:

rateArray = new Array(ratingArray[i][a].value);

So you have to push the elements in to the array instead of creating a new one every time thats also delete the last version.

 var rateArray =[]

for(i=0; i < ratingArray.length;i++)
{

        for(a=0; a < ratingArray[i].length;a++)
        {
                if(ratingArray[i][a].checked)
                {

                         rateArray.push(ratingArray[i][a].value);
                }        
        }

}
share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.