It is very difficult for me to show you my code, as in everything, but I'm trying to do this:
I embed the html code in the DOM in the buy using function .innerHTML, I want to add the click event to the icon that is introduced in this step, since at the moment I know its id. Therefore, after I entered it, I write:
document.getElementById(product.id+"x").addEventListener("click", removeItem);
product.id is created above, and this element is the "X" button, which when pressed will be removed from the screen.
The problem is that this code is run many times, since many elements are displayed on the screen. And when finished, only the latter even made fires when the "X" button is pressed.
Any suggestions?
EDIT:
I can not use jquery in this project.
Here is my code:
function createHTML(targetID, product) {
var target = document.getElementById(targetID);
total = (parseFloat(total) + parseFloat(product.price)).toFixed(2);;
target.innerHTML += '<article class="item" id="'+product.id+'"><img class="item_img" src="../'+product.image+'" width=100 height=100><h1 class="item_name">'+product.name+'</h1><p class="item_description">'+product.desc+'</p><h1 class="item_quantity">Quantity: '+product.quantity+'</h1><h1 class="item_price">£'+product.price+'</h1><i id="'+product.id+'x" class="fa fa-times"></i></article>';
document.getElementById(product.id+"x").addEventListener("click", removeItem, true);
}