0

all,

I have an array that does not seem to get populated. I can see that the length of my array but can't seem to view the data with a simple alert.

My function is called when I click on an href:

I then look for elements that have a class of .jobRole to populate my array and to be displayed with alert. However I'm having difficulties. Thanks for any assistance.

function close() {

    var jobRoleArray = [];

    $(".jobRole").each(function (index) {

        var jobRoleIndex = index;
        var jobRoleID = $(this).attr('id');
        var jobRoleName = $(this).text();

        var roleInfo = {
            "roleIndex": jobRoleIndex,
            "roleID": jobRoleID,
            "roleName": jobRoleName
        };

        queryStr = { "roleInfo": roleInfo };
        jobRoleArray.push(queryStr);

    });

    alert('show length: ' + jobRoleArray.length);

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

        alert('jobRoleIndex: ' + jobRoleArray[i].roleInfo.jobRoleIndex +
                ' jobRoleID: ' + jobRoleArray[i].roleInfo.jobRoleID +
                ' jobRoleName: ' + jobRoleArray[i].roleInfo.jobRoleName +
                ' showCount: ' + i);

    }

}
3
  • What do you see in the alerts? Any errors in the console? Commented Sep 15, 2011 at 23:24
  • You're trying to access the name of the data you put in originally, not the map keys under which the data resides. Commented Sep 15, 2011 at 23:28
  • 1
    It doesn't solve your problem (Dennis has that covered), but you should probably check out the map function, which lets you transform jQuery objects into arrays: var jobRoleArray = $(".jobRole").map(function(jobRole, index) { var jobRoleIndex = index; var jobRoleID = jobRole.attr("id"); ... return queryStr; }); Commented Sep 15, 2011 at 23:29

1 Answer 1

3

You aren't accessing the correct properties; use this instead:

alert('jobRoleIndex: ' + jobRoleArray[i].roleInfo.roleIndex +
        ' jobRoleID: ' + jobRoleArray[i].roleInfo.roleID +
        ' jobRoleName: ' + jobRoleArray[i].roleInfo.roleName +
        ' showCount: ' + i);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This worked. Really had some sleepy eyes and didn't catch this. Thanks again!

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.