114

I've a form as

<form  onSubmit="return disableForm(this);" action="upload.php" method="post" name="f" id="wizecho" enctype="multipart/form-data">
    <input type="file" name="file" />
    <button onClick="return disableForm(this),ajaxUpload(this.form,'wizecho_upload.php', '&lt;br&gt;Uploading image please wait.....&lt;br&gt;'); return false;">
        Upload Image
    </button>
</form>

It is to upload an image.

Here I need to click on the button to upload the image and I've to use onClick event. I want to remove the upload button and as soon as the file is selected at the input, I want to fire the event. How do I do that??

ptamzz
  • 9,235
  • 31
  • 91
  • 147
  • If you are concerned about selecting the same file twice, @applecrusher has a better solution than the selected answer http://stackoverflow.com/a/40581284/1520304 – Will Farley Apr 13 '17 at 21:12

8 Answers8

145

Use the change event on the file input.

$("#file").change(function(){
         //submit the form here
 });
Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
78

You could subscribe for the onchange event on the input field:

<input type="file" id="file" name="file" />

and then:

document.getElementById('file').onchange = function() {
    // fire the upload here
};
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
57

This is an older question that needs a newer answer that will address @Christopher Thomas's concern above in the accept answer's comments. If you don't navigate away from the page and then select the file a second time, you need to clear the value when you click or do a touchstart(for mobile). The below will work even when you navigate away from the page and uses jquery:

//the HTML
<input type="file" id="file" name="file" />



//the JavaScript

/*resets the value to address navigating away from the page 
and choosing to upload the same file */ 

$('#file').on('click touchstart' , function(){
    $(this).val('');
});


//Trigger now when you have selected any file 
$("#file").change(function(e) {
    //do whatever you want here
});
Jaden Baptista
  • 656
  • 5
  • 16
applecrusher
  • 5,508
  • 5
  • 39
  • 89
  • Is this cross-browser compatible? Looks like it's just using `val('')`, which doesn't work in most browsers. – Sean Kendle Apr 28 '17 at 19:04
  • Which browser doesn't it work on that you tried? Try cloning the object with itself it if it is still an issue for you. – applecrusher Jun 12 '17 at 04:56
  • 4
    My issue was using `element.setAttribute("value", "")` If you are not using jQuery you need to use `element.value = ""` to get the file element to really clear properly. – Phil Nov 07 '17 at 09:44
5

vanilla js using es6

document.querySelector('input[name="file"]').addEventListener('change', (e) => {
 const file = e.target.files[0];
  // todo: use file pointer
});
InkieBeard
  • 51
  • 1
  • 4
2

Solution for vue users, solving problem when you upload same file multiple times and @change event is not triggering:

 <input
      ref="fileInput"
      type="file"
      @click="onClick"
    />
  methods: {
    onClick() {
       this.$refs.fileInput.value = ''
       // further logic for file...
    }
  }
Alb Bolush
  • 2,198
  • 2
  • 9
  • 8
1

Do whatever you want to do after the file loads successfully.just after the completion of your file processing set the value of file control to blank string.so the .change() will always be called even the file name changes or not. like for example you can do this thing and worked for me like charm

  $('#myFile').change(function () {
    LoadFile("myFile");//function to do processing of file.
    $('#myFile').val('');// set the value to empty of myfile control.
    });
1
<input id="fusk" type="file" name="upload" style="display: none;"
    onChange=" document.getElementById('myForm').submit();"
>
Cristik
  • 30,989
  • 25
  • 91
  • 127
0
<input type="file" @change="onFileChange" class="input upload-input" ref="inputFile"/>

onFileChange(e) {
    //upload file and then delete it from input 
    self.$refs.inputFile.value = ''
}
Doan Van Thang
  • 989
  • 1
  • 10
  • 21
Pragati Dugar
  • 262
  • 3
  • 7
  • 1
    While this code may provide a solution to the question, it's better to add context as to why/how it works. This can help future users learn and eventually apply that knowledge to their own code. You are also likely to have positive feedback/upvotes from users, when the code is explained. – Syscall Feb 28 '21 at 11:34