I have the following code:
// Iterate through each of the children
for(j = 0; j < len; j++) {
var entry = sortedArray[j];
// Each entryLI is a child of nameOL <ol> element
var entryLI = document.createElement("li");
// Each entryLI should have a unique click listener
entryLI.addEventListener("click", function(event) {entry.onListItemClick()});
// Set the text of the <li> to the name of the entry
var nameNode = document.createTextNode(entry.entryName);
entryLI.appendChild(nameNode);
// Append the entry list item to the ordered list
nameOL.appendChild(entryLI);
}
I am trying to, within this loop, give each list item an event listener that handles click events. However, for some reason, clicking on any of the list items calls the event listener of the last item in the list.
Can anyone explain to me why this is?
I am coming to JavaScript from completely different languages and I am struggling with prototype-based classes.