I have a relatively simple code, I am annding event listeners in a loop, but the loop variable is not available when mapping the results.
for (let index = 0; index < 2; ++index) {
$("#id" + index).on("keyup", function () {
$.ajax({
type: "GET",
url: "https://some-url.com",
dataType: "json",
success: function (data) {
$('#div' + index).append(
$.map(data, function (myData, index) {
return '<a href="#" onclick="selectId(index, myData.id);"> Click me </a>'
}).join());
}
}
);
});
}
The result is:
Uncaught ReferenceError: index is not defined
Edit: if I just use myData.id for example, everything works like a charm Any ideas what am I missing here?