I have a VERY basic problem about JavaScript that is driving me nuts! This should be simple, but it is not. I am writing a JavaScript function, without any other interference such as jQuery and similar.
What I am doing is this:
var myArray = [{name: 'Dolan', age: 34},{name: 'Agroy', age: 133}];
for (var character in myArray) {
var ch = myArray[character];
var divvie = document.createElement('div');
divvie.addEventListener('click', function () { alert(ch.name); });
}
But the problem is that the alert always returns "Agroy" in this case.. it does not provide a "snapshop" of the ch.name as it was when the event was hooked up, but rather how it looks when the entire loop has completed.
That this is not working is just stupid. It is probably trivial.