This is my code:
$(document).ready(() => {
$.getJSON("./data/journeys.json", (data) => {
$.each(data, (i, journey) => {
$("#journeyListings > tbody").append("<tr class='journeyOrder_" + journey.order + "'>" +
"<td>" + journey.originStation + "</td>" +
"<td>" + journey.destinationStation + "</td>" +
"<td>" + journey.startTime + "</td>" +
"<td>" + journey.arrivalTime + "</td>" +
"</tr>")
});
$("tr").on("click", () => {
alert($(this).attr("class"));
});
});
});
On the 'click' method, I am trying to reference the row that has just been clicked (tr
). When I refer to $(this)
, it seems that I am referring to the ajax getJson
method as I am getting undefined
in my alert. How can I reference the element instead?