I want to clear the image which is invalid image format. I've tried multiple times but didn't work. How can I achieve it. Using jQuery 3.6.0
$(':file').change(function(){
var fileArray = this.files;
var validImageTypes = ["image/gif", "image/jpeg", "image/png"];
$.each(fileArray,function(i,v){
type = v.type;
if ($.inArray(type, validImageTypes) < 0) {
alert('Only jpg, jpeg and png is allowed.');
// here i want to clear the image which is selected
};
})
});
I've tried using $(this).val("")
but not working. Please help.
CodePudding user response:
this
points the function called in each.
Use an arrow function to automatically bind to the parent context, (the file element).
$(':file').change(function() {
var fileArray = this.files;
var validImageTypes = ["image/gif", "image/jpeg", "image/png"];
$.each(fileArray, (i, v) => {
type = v.type;
if ($.inArray(type, validImageTypes) < 0) {
alert('Only jpg, jpeg and png is allowed.');
// here i want to clear the image which is selected
$(this).val(""); // semicolon was missing
};
})});