I am printing out an array of products to the screen. There is a delete button being printed along with each product. The code is as follows:-
const output = document.getElementById('output');
function delete_item(){
var id = $(this).attr('data-id');
console.log("ID " + id);
}
let productsArray = [];
$.getJSON('/products', products => {
let outputContent = '';
productsArray = Object.values(products);
productsArray.forEach(product => {
outputContent += `<div data-id="${ product.ID }">
<div id='edit'><i class='fa fa-pencil-square-o' aria-hidden='true'></i></div>
<div id='delete'><i class='fa fa-trash-o' aria-hidden='true'></i></div>
<div id='product'> <span class='name'> <a href=''>${product.NAME}</a></span>
<span class='price'>Price: £${product.PRICE}</span>
</div>`;
});
$('#delete').click(function(){
delete_item();
});
output.innerHTML = outputContent;
});
Currently, I would just like the delete_item function to print out the correct product id. However, the function is never invoked.
Is my .click() function within the scope of delete div, and why is the click() function not being called when clicking the delete div?