0

I have the following javascript code that does not work as I would expect it to. I have a list of checkboxes of which two of the items are "TestDuration" and "AssessmentScores". I'm trying to iterate through the list (which works fine) and have it add the values that are checked to the array.

var SAIndex = 0;
var SSIndex = 0;
var ScoresIndex = 0;
var SubAssessments = [];
var SubAssessmentScores = [];

//Get to the container element
var SSList = document.getElementById("islSubAssessmentScore_container");  

//turn it into an array of the checkbox inputs
SSList = SSList.getElementsByTagName("input"); 

//create a temporary object to store my values
var tempPair = new Object(); 

//iterate through the checkbox lists
for(var i = 1; i < SSList.length;i++) 
{
    //if the value is checked add it to the array
    if (SSList[i].checked) 
    {
        var P = SubAssessments[SAIndex];
        var V = SSList[i].value;
        //tempPair.Parent = SubAssessments[SAIndex];
        tempPair.Parent = P;
        //tempPair.Value = SSList[i].value;
        tempPair.Value = V;
        //show me the values as they exist on the page
        alert(tempPair.Parent + "|" + tempPair.Value); 
        SubAssessmentScores.push(tempPair);

        //show me the values I just added to the array
        alert(SubAssessmentScores.length-1 + "|" + SubAssessmentScores[SubAssessmentScores.length-1].Parent + "|" + SubAssessmentScores[SubAssessmentScores.length-1].Value); 

        //uncheck the values so when I refresh that section of the page the list is empty
        SSList[i].checked = false; 
    }
}

//output the list of objects I just created
for (i = 0;i < SubAssessmentScores.length;i++) 
    alert(i + "|" + SubAssessmentScores[i].Parent + "|" + SubAssessmentScores[i].Value)

Now what happens is that when I iterate through the list I get the following alerts:
-first pass-

StudentID|TestDuration
0|StudentID|TestDuration

-second pass-

StudentID|AssessmentScores
1|StudentID|AssessmentScores

This is what I expect to output... However at the end of the code snippet when it runs the for loops to spit out all the values I get the following alerts...

0|StudentID|AssessmentScores
1|StudentID|AssessmentScores

I can't for the life of me figure out why it's replacing the first value with the second value. I thought it might be using a reference variable which is why I added in the P and V variables to try to get around that if that was the case, but the results are the same.

1 Answer 1

2

This is because you are adding the same variable every iteration of the loop.

Try changing your push like this:

SubAssessmentScores.push({
  Parent: P,
  Value: V
});

That said, I recommend you study a little more javascript and conventions in the language, for example your variable naming is frowned upon because you should only use capital letters on the beginning of a name for constructor functions.

A good book is Javascript the good parts by Douglas Crockford.

Sign up to request clarification or add additional context in comments.

2 Comments

+1; Just to clarify for the OP: there is only one tempPair object, and you keep mutating it. You need to create multiple objects, either by useing the suggestion here, or by moving var tempPair = new Object(); inside the loop.
@apsillers Thanks for that. I used the interim object because I saw that solution in another question. I thought push would create a copy of the object. I didn't realize it actually pushed the reference to the object. Moving the tempPair = new Object(); inside the loop resolved the issue I was having. Thanks both of you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.