15

I have a file-input:

<img id="uploadPreview">
   <div id="changeImage">Change</div>
<div id="customImage">
   <input type="file" id="myfile" multiple style="display:none" onchange="PreviewImage();" />
   <div class='upload blank' id="add-image"></div>
</div>

The function is like below:

var oFReader = new FileReader();
oFReader.readAsDataURL(document.getElementById("myfile").files[0]);

oFReader.onload = function (oFREvent) {
    document.getElementById("uploadPreview").src = oFREvent.target.result;
};

function PreviewImage() {
        var oFReader = new FileReader();
        oFReader.readAsDataURL(document.getElementById("myfile").files[0]);
        $("#uploadPreview").removeClass('hide'); //for manipulating something in the dom
        $('#changeImage').removeClass('hide'); //for manipulating something in the dom
        $("#customImage").addClass('hide'); //these are for manipulating something in the dom

        oFReader.onload = function (oFREvent) {
            document.getElementById("uploadPreview").src = oFREvent.target.result;
        };
    };

Everything works perfect. Now I have a Change button. I want if someone clicks on it then previous uploaded file-details to be gone. The function is something like below:

$('#changeImage').click(function(){
  $('#uploadPreview').addClass('hide');
  $('#customImage').removeClass('hide');
  //here I want to remove/clear the details about the already previous uploaded file in the 'file-input'. So the same image can be shown if someone clicks it for once again. 

});

Can you help on this?

DontVoteMeDown
  • 21,122
  • 10
  • 69
  • 105
the.big.lebowski
  • 279
  • 1
  • 3
  • 12

3 Answers3

20

If you wish to keep data in other form fields, you can use

HTML

    <input type='file' id='yourid' />

jQuery

    $('#yourid').val('');

this will clear your uploaded file from input tag

Aerious
  • 490
  • 3
  • 11
  • 5
    Or in pure javascript: `let file_picker = document.getElementById('yourid'); file_picker.value = '';` – P.M. Apr 04 '17 at 18:33
10

If you put your file input inside a <form> tag you can use the built in .reset() method.

HTML:

<img id="uploadPreview">
   <div id="changeImage">Change</div>
<div id="customImage">
   <form id="fileform">
      <input type="file" id="myfile" multiple style="display:none" onchange="PreviewImage();" />
   </form>
   <div class='upload blank' id="add-image"></div>
</div>

JS:

$('#changeImage').click(function(){
   $('#uploadPreview').addClass('hide');
   $('#customImage').removeClass('hide');
   // Reset the form
   $("#fileform")[0].reset();
});

JS without jQuery:

var resetButton = document.getElementById('resetButton');
var fileInput = document.getElementById('fileInput');
var form = document.getElementById('form');

resetButton.addEventListener('click', function () {
    // resetting the file input only
    fileInput.value = null;

    // alternatively: resetting the entire form (works in older browsers too)
    form.reset();
});
sudee
  • 773
  • 9
  • 14
3

In React I experienced this issue for file inputs on chrome-based browsers. I fixed it with a combination of value="" and autoComplete={"new-password"}

<input
  value=""
  type="file"
  autoComplete={"new-password"}
  onChange={e => pickHandler(e)}
/>
chidimo
  • 2,684
  • 3
  • 32
  • 47
  • 1
    In Vue I was able to fix it similarly by setting the value of the input to an empty string. – DrCord Dec 11 '20 at 00:04