I am trying to change the row of the table according to the button clicked. The row changes for the first time button is clicked, but after that row value doesn't change. Also, the event listner is removed after button changes.
HTML:
<% if(post.status === 1){ %>
<input type="button" class="btn btn-danger" value="Disapprove" id="disapproveBtn-<%= i %>">
<input type="button" class="btn btn-primary" value="Send to Moderation" id="moderateBtn-<%= i %>">
<% } %>
jQuery:
$("[id|='disapproveBtn']").click(function (e) {
console.log("CLICKED");
var trIndex = $(this).closest('tr').index();
var tr = $(this).closest('tr');
var postId = $(this).closest('tr').find("#postId").text().trim();
$.post('/admin/disapprove/' + postId, (data) => {
console.log(tr);
console.log(data);
tr.html(`
<td>
${data.post.firstName}
</td>
<td>
${data.post.lastName}
</td>
<td>
${data.post.userId}
</td>
<td>
<div id="postId">
${data.post.id}
</div>
</td>
<td>
<a href="/profile/post/${data.post.id}">Here</a>
</td>
<td>
${data.post.status}
</td>
<td>
<input type="button" class="btn btn-success" value="Approve" id="approveBtn-${trIndex}">
<input type="button" value="Send to Moderation" class="btn btn-primary" id="moderateBtn-${trIndex}">
</td>
`)
});
});