I'm creating a simple shopping cart with javascript, and what would a shopping cart without a button to remove an item be?
The cart is created on-the-fly with javascript, so the elements won't exist on page load. However, this shouldn't be a problem with this?
$(".removeFromCart").on("click",function(e){
alert("GO AWAY PLEASE..");
var tuoteID = $(this).attr("data-id");
deleteFromCart(tuoteID);
});
The element (created on the fly) simply looks like this:
<a href="#" class="removeFromCart" data-id="2">×</a>
But it won't trigger. If I replace .replaceFromCart
with, for example, p
, it will trigger every time I click on a paragraph.
This isn't the first time I'm having problems with .on()
and it always takes half of my hair to solve, so maybe someone with more experience could point me what I'm doing wrong.