I have this table:
<html>
<body>
<table id="user-table">
<tr >
<th>First Name</th>
<th>Last Name</th>
</tr>
</table>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"</script>
<script src="get-users.js"></script>
<script src="delete-user.js"></script>
</body>
</html>
And this is get-users.js
file where i populate the table:
let $userTable = $("#user-table");
let $xhr = $.ajax({
method: "GET",
url: "http://localhost:8080/user-rest-api",
dataType: 'json'
});
$xhr.done(function (data) {
for (let user of data) {
$userTable.append(`
<tr>
<td> ${user.firstName}</td>,
<td> ${user.lastName} </td>
<td> <button class = 'delete-btn' value=${user.id}>Delete</button> </td>
</tr>
`);
}
});
And in delete-user.js
i just want to print user id, this is what i tried:
$('#user-table').on('click', '.delete-btn', function () {
let userId = $(".delete-btn").val();
console.log("User ID : " + userId);
});
But it only prints first id, how can i dynamically get value based on button that is clicked?