I have a page getting JSON data via AJAX. I've managed to parse the data and create table rows from it. I'm now trying to add .onclick
listeners to those rows using the following code:
function addResultsRows(tbody, jsonResults) {
for (var i in jsonResults) {
var tableRow = tbody.insertRow(0);
var currRow = jsonResults[i];
tableRow.onclick = (function () {
window.location = '/otherscript?cusnum=' + currRow['cusnum'];
});
var currCell = tableRow.insertCell(0);
currCell.innerHTML = currRow['cusnum'];
}
}
etc.
The problem I'm running into is that ALL of the rows are ending up with listener functions that use the currRow['cusnum']
value from the LAST row added to the table.
JavaScript isn't (and won't likely ever be) my forte - only doing this because there isn't anyone else to do front-end code. Is the problem something to do with using an anonymous function in a loop?