I am trying to hide a button that i clicked on to upload a image after succesfull upload.
this is my code:
$(document).on('click', '.upload-image-click', function() {
var token = $('input[name="csr"]').attr('value');
var formData = new FormData();
formData.append('image_file', $(this).siblings('input[name=image_file]')[0].files[0]);
$.ajax({
url: '/image-upload-ajax/',
data : formData,
method : 'POST',
headers: {
'X-CSRFToken': token
},
success: function (data) {
$(this).hide();
console.log(data);
},
error: function (data) {
console.log('error');
},
contentType:false,
cache: false,
processData:false,
});
})
You may noticed, on success block, i have added this line $(this).hide();
but it is not working.
Everything is working great except hiding the button. Can anyone help me to get it done?
2
Answers
Instead, use the element selector. the scope of
this
changes once you are in the success function scope. This should work:Your Ajax callback can not use
this
anymore.Instead create a unique class on clicked image and also save it to variable:
Then use that variable to delete that image:
Example of unique class:
SO your code should be: