I have a list of saved items that the user can delete if he wants. They are inside a #div, a delete button next to each item. If I press the delete button after the favorite items list page is loaded, it works fine.
But, if I want to delete another item, as I press on the delete button, the delete function would not run.
There is my AJAX:
$(".substitut").on('click', function(event) {
event.preventDefault();
var product = $(this).val();
var url = '/register/delete/';
$.ajax({
url: url,
type: "POST",
data:{
'product': product,
'csrfmiddlewaretoken': $('input[name=csrfmiddlewaretoken]').val()
},
datatype:'json',
success: function(data) {
if (data['success'])
$("#fav_list").load(" #fav_list > *");
}
});
});
and my HTML:
<button type ='button' class=' btn substitut' value='{{ saved.id }}'>{% csrf_token %}<i class='fas fa-trash-alt'></i></button>
One quick trick I found was to add location.reload(true)
; after the div refresh but the point of all of this is not having my page refreshing.