0

i'm new to javascript and jquery and was wondering if someone could let me in on why this isn't working correctly.

i have a drop-down box that a user selects a value from, then "Processes." When processed the value of the drop-down as well as a textbox is stored in an array. I want the user to be able to then basically store the same drop-down selection and textbox data in the array again but now in a new value pair.

First store would be TestArray[0][0] = "Textbox Value"

If "Processed" again, it would be TestArray[1][0] = "Textbox Value"

that way I can parse through later and figure how many times the user "Processed" the drop-down selection;

var oneClickReport = $("#reportName").val();
    if(oneClickReport == "Sample Report One"){
        var arrayOneCount = reportOneArray.length;
        var totalHouseholds = 0;
            $("#reportChecks span:visible").each(function(){            
                if($(this).find(':checkbox').prop('checked')){
                    var HHName = $(this).text();
                    reportOneArray.push(HHName);
                    arrayTest[arrayOneCount][totalHouseholds] = HHName;
                }
            totalHouseholds += 1;
            });
            for(i = 0; i < arrayOneCount; i+=1){
                alert(arrayTest[0][i]);
            }
    }

But when trying to "Process" for the second time, I receive the error of;

SCRIPT5007: Unable to set property '0' of undefined or null reference 

on line;

arrayTest[arrayOneCount][totalHouseholds] = HHName;

2 Answers 2

1

You need to initialize your array. I'm not sure what exactly you want to do but you need an array like this

var arrayTest = []

And you will need to initialize subsequent value like

arrayTest[1] = []

Then you can access your array

arrayTest[1][0] = []

I made an example for you

var oneClickReport = $("#reportName").val();
var arrayTest = [] # You may need to put this elsewhere
if(oneClickReport == "Sample Report One"){
    var arrayOneCount = reportOneArray.length;
    var totalHouseholds = 0;
        $("#reportChecks span:visible").each(function(){            
            if($(this).find(':checkbox').prop('checked')){
                var HHName = $(this).text();
                reportOneArray.push(HHName);

                if(!arrayTest[arrayOneCount]){ arrayTest[arrayOneCount] = []; }

                arrayTest[arrayOneCount][totalHouseholds] = HHName;
            }
        totalHouseholds += 1;
        });
        for(i = 0; i < arrayOneCount; i+=1){
            alert(arrayTest[0][i]);
        }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for your answer, I think i've almost got it. One thing I ran into that I definitely wasn't thinking about is that if the user initially selects more than 1 checkbox to be added, the arrayOneCount value will not be 1, but however many was selected. is there a way for me to just check if the arrayTest[0] or arrayTest[1] etc. has been initialized and populated? I really just want something like if the user process "Report One" first time it's stored in arrayTest[0] for all selected checkboxes, then if they process again arrayTest[1] etc. Thanks again
You can acheive this with the length property of the array. if(arrayTest[arrayOneCount].length > 0){ #There is something in the arrayTest[arrayOneCount] } else{ #There is nothing in the arrayTest[arrayOneCount] }
0

your problem with var arrayOneCount = reportOneArray.length; and you're not changing this value

1 Comment

the value will be incremented everytime the function is called. It will re-count the new values that were added to the array and update that way

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.