I want to delete the parent of clicked element. I use a sweet alert to get alert first and after I call an AJAX function and I want to get the element in the success function:
This is my function:
function removeImage(id) {
var element = $(this).closest('.dropzone');
swal({
title:"Delete",
text: "delete",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes !",
cancelButtonText: "No, cancel",
closeOnConfirm: false, closeOnCancel: false
}, function(isConfirm){
if (isConfirm) {
$.ajax({
type: "POST",
data: {id: id},
url:'ajax/deleteimage.php',
success : function(data){
if(data == 'ok'){
swal({
title:"Delete",
text: "delete",
type: "success",
confirmButtonColor: "#AEDEF4",
confirmButtonText: "Ok",
closeOnConfirm: true,
}, function(isConfirm){
$.when($('.dropzone').find("#"+id).parent().fadeOut())
.done(function() {
$('.dropzone').find("#"+id).parent().remove();
});
var n_div = $('.dz-image-preview').length-1;
if (n_div === 0) {
$('.dz-message').css("opacity",'1');
}
});
}else{
swal("Error, try again", "", "error");
}
}
}); // end ajax call
} else {
swal("Cancel", "", "error");
}
});
}
I can't change the $('.dropzone')
in the success function with the variable element.