Can somebody tell me how to append HTML code that have button element and make that button functional? Why is my function not working on appended button?
Here's my code:
<div id="attachments">
<input id="add-attachment" type="button" value="Dodaj kolejny ✚" class="button-custom-3">
<div class="hr-custom-1"></div>
<div>
<input type="button" value="Przeglądaj" class="button-custom-1">
<span>Przykładowa nazwa załączonego pliku.jpg</span>
<input type="button" value="Usuń ✖" class="button-custom-2 button-remove-attachment">
<div class="hr-custom-1"></div>
</div>
<div>
<input type="button" value="Przeglądaj" class="button-custom-1">
<span>Przykładowa nazwa załączonego pliku.jpg</span>
<input type="button" value="Usuń ✖" class="button-custom-2 button-remove-attachment">
<div class="hr-custom-1"></div>
</div>
JavaScript functions:
$(".button-remove-attachment").click(function(){
$(this).parent().remove();
});
$("#add-attachment").click(function(){
var element = $("<div><input type='button' value='Przeglądaj' class='button-custom-1'><span>Przykładowa nazwa załączonego pliku.jpg</span><input type='button' value='Usuń ✖' class='button-custom-2 button-remove-attachment'><div class='hr-custom-1'></div></div>");
$("#attachments").append(element);
});
Please check it on jsFiddle: https://jsfiddle.net/6f9xhr64/