Having a bit of trouble trying to select an element based on its class using $(".class"), and I can't seem to figure out why it's not working.
So, the deal is - I have this image element that should appear when a function gets triggered:
$("#container").append("<img class='removeIcon' src='images/remove.png' remove='"+$(this).val()+"' />");
This part works perfectly fine. The image shows up whenever the function runs, even if it's repeated multiple times. And each image has the correct value assigned, so everything seems in order with the element itself.
However, the issue lies here...
$(".removeIcon").click(function() {
alert();
console.log("Clicked!");
});
I don't know for sure why it's not functioning as expected. Could it be because the images are added later on? Or am I just missing something obvious? Whenever I click on any image with that class, there's no pop-up or log message displayed.
I even checked by typing $(".removeIcon") into the console, and it does show all the images with that class. So, what am I missing here? Can you actually attach a click event to a class selector?
Appreciate any help you can offer! Thank you!