I have created a table and want to get an id on a td click. The data is population through an ajax call and I an want an id on when delete is clicked. So its on the fly the elements are added.
$.ajax({
url: 'http://localhost:8080/getUsers,
type: 'GET',
success: function (data, textStatus, jqXHR) {
$.each(data, function (index, result) {
$("<tr></tr>").attr("id",result.user_id+"userTr").appendTo("#userTBody");
$("<td></td>").attr("id",result.user_id+"userName").appendTo("#"+result.user_id+"userTr");
$("#"+result.user_id+"userName").html(result.username);
$("<td></td>").attr("id",result.user_id).appendTo("#"+result.user_id+"userTr");
$("#"+result.user_id).html('Delete');
$("#"+result.user_id).css('cursor', 'pointer');
$("#"+result.user_id).addClass("delete");
});
},
error: function() {
alert('err')
}
});
$('.delete').on('click', function () {
alert($(this).attr('id'));
});
I have also tried with this.id but nothing happens.