0

I have assigned a variable with arrays inside array i.e.

var encounter_names = new Array();
encounters.encountersDB.each(function(encounter) { encounter_names.push(new Array(encounter.key, encounter.name))})

this is my for loop statement mentioned as below

for (var i = 0; i < encounter_names.length; i++) {
    window['li' + i] = $("li[key='" + encounter_names[i][0] + "']")
    window['li' + i] = $("<li key='" + encounter_names[i][0] + "' class='list'><a class='arrow_'+ encounter_names[i][0] +'" + encounter_names[i][0] + "' href='#'>" + encounter_names[i][1] + "</a></li>")
    $('.arrow_' + encounter_names[i][0] + '').click(function (event) {
        encounters.show(window['key' + i]);
        event.preventDefault();
    })
    encounters_list.append(encounter_names[i][0])
}

When I click an href link I am get the following error in console log:

TypeError: Result of expression 'encounter_names[i]' [undefined] is not an object.

Any suggestions are always welcomed.

3
  • sorry instaed of list_of_encounters i should be encounter_names Commented Sep 21, 2010 at 14:10
  • Does my edit reflect what you meant? Commented Sep 21, 2010 at 14:12
  • In the first code section you add values from something named "encounters" to the array encounter_names. In the second code section where did the variable "list_of_encounters" come from? - Never mind, you updated while I was typing. Commented Sep 21, 2010 at 14:14

1 Answer 1

1

I think encounters.encountersDB is an object, not a html object?
The problem is this: The each function gets two arguments: the current index and the element. So you just have to change this to:

var encounter_names = []; // short form for new Array()
$.each(encounters.encountersDB, function(index, encounter) {
  encounter_names.push([encounter.key, encounter.name]);
})
Sign up to request clarification or add additional context in comments.

Comments

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.