I have a table from which I want to handle click events on each row, depending on the contents of a table cell. The table is (currently) filled dynamically from a static array, although later it will be filled from a database:
var routeTable = document.getElementById('routeTable');
var routes = [
['..', '..', '..', "url_1"],
['..', '..', '..', "url_2"],
['..', '..', '..', "url_3"],
['..', '..', '..', "url_4"],
]
for (r = 0; r < routes.length; r++) {
route = routes[r];
row = routeTable.insertRow(r+1);
....
....
row.addEventListener("click", function () { display_gpx(route[3]);
});
}
The trouble is that the parameter passed to the function display_gpx() is always the value "url_4" (from the last route in the table).
I found that the elements for clicking must exist when the document is created , so I looked here (the pure js version) and tried adding
row.className = "myClick";
and changing the eventListener to
document.addEventListener("click", function (e)
{
if (e.target.classList.contains("myClick"))
display_gpx(route[3]);
},false);
but the events don't fire at all.
I think I've solved this before, but returning to javascript after a long break and can't remember the solution!