I have an empty table to which I'm adding rows via jQuery using:
$('#table > tbody:last').append('<tr id="' + symbol.Code1 + '"><td>' + symbol.Code1 + '</td><td>' + symbol.Code2+ '</td><td>' + symbol.Code3+ '</td></tr>');
Everything is OK but when I implement:
$("#table tr").click(function(e) {
alert(this.id);
});
nothing happens.
You add rows dynamically after you have bound the event to the existing ones. You may use delegated event approach to fix the problem:
You need event delegation you can use on to bind the click event for dynamically added elements. The way you are binding with click will apply on existing element but not elements which are added later.
You can limit the scope for
on
by giving closest parent selector either by id or by class of parent.Delegated events
You have to use the
.on
method