I have a function where I get some data, and insert it into a row of a table.
I then set up a click event on the row so that when it is clicked, it navigates to a URL specified in my data object:
$(data).each(function(i, item) {
var row = $("<tr></tr>");
tbody.append(row);
// Add some cells into the row...
var firstNameCol = $("<td>" + item.FirstName + "</td>");
row.append(firstNameCol);
// Set up click handler
row.click(function() {
window.location.href = item.DetailsURL;
});
});
Now this code works fine, but it has made me realise that I'm not sure how the events work.
The function contains a reference to item, which is a variable local to my for loop of items. When the user clicks on the row, how does it know which item it is looking at? Is it somehow cached when the click function is registered?
Secondly, is this method really bad? Am I forcing it to do a load of work behind the scenes?