I bind data using jQuery to a HTML table. I need to pass data using a function call. Here is my code; please note that I get data from json and bind that data to table using jQuery.
result.data.resultSet.forEach(user => {
const $tr = $('<tr>');
$tr.append($('<td>').text(user.username));
$tr.append($('<td>').text(user.firstname));
$tr.append($('<td>').text(user.lastname));
$tr.append($('<td>').text(user.email));
$tr.append($('<td>').text(user.usertype));
const profileURL = "./UserUpdate?name=" + user.username + "&firstname=" + user.firstname + "&usertype=" + user.usertype;
var ondelete = onDelete(user.id);
const $td = $('<td>').append($('<a>').attr('href', profileURL).append($('<img>').attr('src', '/images/Common UI Assets/Icon-16.png')))
.append($('<a>').attr('onclick', onDelete(user.id)).append($('<img>').attr('src', '/images/Common UI Assets/Icon-16 _Delete.png')));
$tr.append($td);
$('#usertable tbody').append($tr);
});
Here in this section .append($('<a>').attr('onclick', onDelete(user.id))
I'm calling a function onDelete(user.id) and pass user.id. But problem is I did not click any that image or row but it is calling every time when load data. I want to get that user data which row I click.
Here is my ondelete function I console it .
function onDelete(item) {
console.log(item);}
onclick2. use$('<a>').on('click', function () { /* actual command in here */ });