Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

122
Views
How to clear image input in each loop of jQuery?

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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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(""); 
    };
})});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!