I'm building a dynamic table, assigning each row a unique ID based on the value of the counter, and trying to display the row id in an alert box when a row is clicked:
$(function() { for (i=0; i<5; i++) { var row_id = 'row' + i; var rw = '<tr id="' + row_id + '"><td>row ' + i + '</td></tr>'; $('#t1').append (rw); $('#t1 tr#' + row_id).click( function() { alert(row_id); } ); } }); The problem is that although I get a different row_id for the id of each row, the alert box is always displaying "row4". I understand why this is happening; this was the value at the last pass through the loop. What can I do to have it display the correct value?