enter code here
I have a list of items from a database displayed thanks to a for-loop
in Django.
I have a "save button" next to each one to save them to a favorite database thanks to an AJAX call. Everything works like a charm.
But I want the "save button" to .hide()
whenever the AJAX call is a "success". But if I do so, every "save button" from the page is hidden.
So I tried to use .parent()
or .closest()
to select only the <div>
where my "save is button" with no luck.
My HTML:
{% for sub_product in products %}
<div class='sub_button'>
<form class="add_btn" method='post'>{% csrf_token %}
<button class='added btn' value= '{{product.id }} {{ sub_product.id }}' ><i class=' fas fa-save'></i></button>
</div>
{% endfor %}
My AJAX:
$(".added").on('click', function(event) {
event.preventDefault();
var product = $(this).val();
var url = '/finder/add/';
$.ajax({
url: url,
type: "POST",
data:{
'product': product,
'csrfmiddlewaretoken': $('input[name=csrfmiddlewaretoken]').val()
},
datatype:'json',
success: function(data) {
if (data['success'])
$(this).parent('.sub_button').hide();
}
});
});